Skip to content

Structured Output fails with enum field in pydantic schema. #65

@MeerrettichMeister

Description

@MeerrettichMeister

#54 enabled the expected support for pydantic schemas for structured output.

However the feature works just partially since when including an enum field in the pydantic class the request fails due to unrecognized/unsupported fields in the request body to the oci endpoint.

oci_chat = ChatOCIGenAI(
    model_id="google.gemini-2.5-flash",
    service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
    compartment_id=...,
)


class Domain(Enum):
    NATURE = "NATURE"
    TECHNICAL = "TECHNICAL"
    HISTORICAL = "HISTORICAL"


class Formatted(BaseModel):
    answer: str
    user_request: str
    domain: Domain


structured = oci_chat.with_structured_output(Formatted, method="json_schema")
structured_response = structured.invoke(
    [HumanMessage("Tell me something about the statue of liberty")]
)

**print(structured_response)**

Running the provided code returns the endpoint error.

Pydantic generates a json schema where the enum is placed behind a $def field and the attributes then just reference the field with $ref
It seems that the generative ai endpoint is not able to parse that schema.

For the sake of future proving I would advise to test the pydantic parsing with more complex schemas i.e. nested schemas, enum types, literalt types etc.

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