Skip to content

Request and response types

Nested request parameters are Python TypedDicts.

For example, the user message in the following chat.completions.create() request is a ChatCompletionUserMessageParam, which has a base type of TypedDict:

from openai import OpenAI

client = OpenAI()

completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Can you generate an example JSON object describing a fruit?",
        }
    ],
    model="gpt-3.5-turbo-1106",
    response_format={"type": "json_object"},
)

File upload request types

Request parameters that correspond to file uploads can be passed as bytes, a PathLike instance, or a tuple of (filename, contents, media type).

from pathlib import Path
from openai import OpenAI

client = OpenAI()

client.files.create(
    file=Path("input.jsonl"),
    purpose="fine-tune",
)

The async client uses the same interface. If you pass a PathLike instance, the file contents will be read asynchronously automatically.

Response types

Responses are Pydantic models that include their helper methods for things like:

Tip

Typed requests and responses enable type checking, autocompletion, and hover-help documentation in editors that support those features. In Visual Studio Code, for example, you can enable type checking in Pylance by setting python.analysis.typeCheckingMode to basic as described in that article's Settings and Customization section.