Skip to content

OpenAI Responses Streaming API

​OpenAI's Responses API is also capbable of streaming responses.

See the OpenAI Documentation for details.

API Tour

Here is an example calling the Streaming Responses API to ask a simple question.

c
#include "ioto.h"

void example(void)
{
    Url   *up;
    cchar *vectorId = "PUT_YOUR_VECTOR_ID_HERE";
    char  buf[1024];

    /*
        SDEF is used to catentate literal strings into a single string.
        SFMT is used to format strings with variables.
        jsonParse converts the string into a json object.
     */
    Json *request = jsonParse(SFMT(buf, SDEF({
        model: 'gpt-4o-mini',
        input: 'What is the capital of the moon?',
        tools: [{
            type: 'file_search',
            vector_store_ids: ['%s'],
        }],
    }), vectorId), 0);

    up = urlAlloc(0);
    up = openaiStream(request, (UrlSseProc) aiStreamCallback, 0);
    jsonFree(request);
    if (up == NULL) {
        rError("example", "Cannot connect to OpenAI");
        return;
    }
    urlWait(up);
    urlFree(up);
}

static void streamCallback(Url *up, ssize id, cchar *event, cchar *data, void *arg)
{
    printf("id: %ld\nevent: %s\ndata: %s\n", id, event, data);
}

The openaiStream API takes a JSON object which represents the Responses parameters. The SDEF macro is a convenience to make it easier to define JSON objects in C code. The SFMT macro expands printf style expressions. The jsonParse API parses the supplied string and returns an Ioto Json object which can be passed to the openaiStream API.

The API returns a HTTP status of which 200 indicates success. The callback function is called for each event in the stream. The id is the event id. The event is the event type. The data is the event data. The arg is the user data passed to the openaiStream API.

Consult the Responses API for parameter details.

See the ai app in the Ioto Agent source download for an example stream.html web page that uses the Responses API in streaming mode.

References

Consult the OpenAI documentation for API details: