Skip to content

Conversation

@paxiaatucsdedu
Copy link
Member

Problem

with_structured_output(..., method="json_schema") fails when using Pydantic models containing Enums or nested models because OCI Generative AI API doesn't support JSON Schema $ref and $defs keywords.

Error:

ServiceError: Invalid JSON payload received. Unknown name "$defs" at 'generation_config.response_schema'
Solution

See issue #65

Solutions

  • Added OCIUtils.resolve_schema_refs() to recursively inline all $ref references and remove $defs before sending schemas to OCI API
  • Integrated schema resolution into with_structured_output() for json_schema method
  • Added unit test: test_with_structured_output_json_schema_nested_refs() to verify fix with nested models and enums

Testing

For Literal Pydantic data structure:

Input:

class Status(BaseModel):
    status: Literal["pending", "running", "done"]

class Formatted(BaseModel):
    answer: str
    user_request: str
    status: Status


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

print(structured_response)

Output:
answer='The Statue of Liberty is a colossal neoclassical sculpture on Liberty Island in New York Harbor in New York City. The copper statue, a gift from the people of France to the people of the United States, was designed by French sculptor Frédéric Auguste Bartholdi and built by Gustave Eiffel. It was dedicated on October 28, 1886.' user_request='Tell me something about the statue of liberty' status=Status(status='done')

For Enum Pydantic data structure:

Input:

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


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


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

print(structured_response)

Output:
answer='The Statue of Liberty was a gift from the people of France to the United States and was dedicated on October 28, 1886. It is a symbol of freedom and democracy.' user_request='Tell me something about the statue of liberty' domain=<Domain.HISTORICAL: 'HISTORICAL'>

For nested reference Pydantic data structure:

Input:

class Color(Enum):
    RED = "RED"
    BLUE = "BLUE"
    GREEN = "GREEN"

class Item(BaseModel):
    name: str
    color: Color  # ← Creates $ref to Color

class Response(BaseModel):
    message: str
    items: List[Item]  # ← Array with $ref inside

# Test
structured_chat = chat.with_structured_output(Response, method="json_schema")
structured_response = structured_chat.invoke([
    HumanMessage("Tell me about 3 colored objects")
])

print(structured_response)

Output:
message='Here are 3 colored objects.' items=[Item(name='Apple', color=<Color.RED: 'RED'>), Item(name='Sky', color=<Color.BLUE: 'BLUE'>), Item(name='Grass', color=<Color.GREEN: 'GREEN'>)]

Added resolve_schema_refs to OCIUtils to inline $ref and $defs in JSON schema, as OCI Generative AI does not support these features. Updated ChatOCIGenAI to use this utility before passing schemas to the provider. Added a unit test to verify correct handling of nested $ref in structured output schemas.
@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Dec 5, 2025
@YouNeedCryDear YouNeedCryDear merged commit e386b48 into oracle:main Dec 5, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants