Skip to content

Conversation

RobinPicard
Copy link
Contributor

Addresses #1722

This PR adds 2 class methods to the JsonSchema class to check whether an object is a JSON schema type and to convert such an object to a different JSON schema type

This change has 2 main objectives:

  • Provide intercompatibility between all different supported JSON schema types such that the user could provide any of them even if the model they use support only one in the list. This can be useful for some API-based models (Gemini)
  • Simplify the code in the format_output_type methods of the model type adapters. We want to push the complexity upstream such that the models only have to deal with the JsonSchema class instead of having to treat different JSON schema types itself

@RobinPicard RobinPicard self-assigned this Sep 25, 2025
@RobinPicard RobinPicard requested a review from rlouf September 25, 2025 13:10
@RobinPicard RobinPicard force-pushed the add_json_type_conversion branch 4 times, most recently from 6a8bff8 to abf39b8 Compare September 25, 2025 17:28
Comment on lines +374 to +381
target_types: List[Literal[
"str",
"dict",
"pydantic",
"typeddict",
"dataclass",
"genson",
]],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not have different methods?

Copy link
Contributor Author

@RobinPicard RobinPicard Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be a more convenient interface to have only one method through which one can list all the types they supports.

For instance:

if JsonSchema.is_json_schema(output_type):
    return JsonSchema.convert_to(
        output_type,
        ["dataclass", "typeddict", "pydantic"]
    )
)

@RobinPicard RobinPicard force-pushed the add_json_type_conversion branch from 96404c4 to 672cdd1 Compare September 26, 2025 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide type conversion among json schema and multiple-choice output types
2 participants