Skip to content

Pydantic model to appropiate schema for structured data #2557

@Scolymus

Description

@Scolymus

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

  • This is a feature request for the Python library

Describe the feature or improvement you're requesting

Is there any function we could use to add a pydantic model into a structured data?

For instance, if I call client.chat.completions.create, and send as response_format:

response_format = {
                "type": "json_schema",
                "json_schema": {
                    "name": my_pydantic_schema.__name__,
                    "schema": my_pydantic_schema.model_json_schema(),
                    "strict": True,
                    "description": "Check messages for more details.",
                }
            } 

Is there a way to add a valid schema in the schema key? I'm asking this because I have noticed that:

  • Your pydantic model needs extra forbid (or additionalProperties=False after returning the model_json_schema()
  • Your pydantic model must have all fields as required, without optional

However, when using a pydantic model that one of its attributes is an enum or another pydantic model, you can see that the attribute has a $ref that links to the $refs object, which contains one key for every reference. However it seems that additionalProperties must be false in every of these childs, and even if these models contains all of its items as required, the "required" in the first level of my_pydantic_schema.model_json_schema() needs the required from the $refs.

Maybe some function that does some recursive action?

schema = my_pydantic_schema.model_json_schema()
_create_strict_model(schema)

def _create_strict_model(self, obj: dict[str, Any]) -> None:
    """
    Create a strict version of the output schema for OpenAI structured outputs.
    """
    obj["additionalProperties"] = False
    
    # OpenAI strict mode: all properties must be required
    if "properties" in obj:
        #do stuff for required

    # Process definitions recursively
    if obj.get("$defs"):
        for value in obj["$defs"].values():
            self._create_strict_model(value)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions