Using the library

JSON requests and responses

protoverta.convert_request() and protoverta.convert_response() take a decoded JSON body, its protocol and the target protocol ("chat", "messages" or "responses", or a Protocol). They return a new body; the input is not modified.

from protoverta import ConversionError, convert_request

try:
    responses_request = convert_request(chat_request, "chat", "responses")
except ConversionError as error:
    print("not portable:", error)

Bare conversion omits tool and choice metadata that only the original request knows. To restore it on Responses output, bind the original request with protoverta.bind_response_context(). The gateway does this for you.

Streams

protoverta.StreamTranscoder converts a server-sent-events stream from one protocol to another. Feed it the raw upstream bytes as they arrive and call finish() at the end of the stream:

from protoverta import StreamError, StreamTranscoder

transcoder = StreamTranscoder("chat", "responses")
try:
    for chunk in upstream_chunks:          # bytes, split anywhere
        client.write(transcoder.feed(chunk))
    client.write(transcoder.finish())
except StreamError as error:
    ...  # invalid, interrupted or unsupported stream

Translating Chat Completions or Responses into Messages needs messages_usage_policy="deferred": Messages announces input usage in its first event, which those protocols only report at the end. Initial counts are then provisional and corrected in message_delta.

The lower-level StreamDecoder turns events into portable Delta items and StreamEncoder turns deltas into events.

Capabilities, catalogs and token counts

The complete reference is in Python API.