Skip to content

messages.create() takes a JSON schema dict but messages.stream() takes a Pydantic model #1071

@simonw

Description

@simonw

Just ran into this slight inconsistency in the API relating to structured content support.

Here's the messages.create() idea of output_format=:

output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,

Which uses:

class BetaJSONOutputFormatParam(TypedDict, total=False):
schema: Required[Dict[str, object]]
"""The JSON schema of the format"""
type: Required[Literal["json_schema"]]

So it accepts a Python dictionary that represents a JSON schema.

But the messages.stream() method:

output_format: Optional[type[ResponseFormatT]] | Omit = omit,

Later does this:

if is_given(output_format) and output_format is not None:
adapted_type: TypeAdapter[ResponseFormatT] = TypeAdapter(output_format)

TypeAdapter is a Pydantic concept, so this has the effect of only allowing Pydantic types to be passed to messages.stream().

It's confusing and inconsistent that messages.create() requires a JSON schema dictionary but messages.stream() instead requires a Pydantic model.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions