Skip to content

Adding support for typed dict in schema conversion#176

Draft
lucyliulee wants to merge 4 commits intomainfrom
lucy/schema_typed_dict
Draft

Adding support for typed dict in schema conversion#176
lucyliulee wants to merge 4 commits intomainfrom
lucy/schema_typed_dict

Conversation

@lucyliulee
Copy link
Contributor

What does this PR do?

Adds TypedDict support to the schema converter, enabling proper JSON schema generation for tools with nested object parameters. This fixes a critical issue where OpenAI models reject list[dict] parameters because the generated schema lacks property definitions required by OpenAI.

Problem:

  • Using list[dict] in tool parameters generates {"type": "object"} without properties
  • OpenAI's API rejects this with: additionalProperties is required to be supplied and to be false
  • This caused tools to fail with GPT models while working with Gemini

Solution:

  • Add TypedDict detection and schema generation in schema_converter.py
  • Generate proper schemas with properties, required, and additionalProperties: false
  • Add warnings when developers use dict or list[dict] types (pointing to TypedDict as the fix)
  • Propagate strict flag through recursive type resolution

Example:

  # Before (broken with OpenAI)                                                                                                                                              
  items: Annotated[list[dict], "Items to order"]                                                                                                                             
  # Generated: {"type": "array", "items": {"type": "object"}}  ❌                                                                                                            
                                                                                                                                                                             
  # After (works with all models)                                                                                                                                            
  class MenuItem(TypedDict):                                                                                                                                                 
      menu_item_id: str                                                                                                                                                      
      quantity: int                                                                                                                                                          
                                                                                                                                                                             
  items: Annotated[list[MenuItem], "Items to order"]                                                                                                                         
  # Generated: {"type": "array", "items": {"type": "object", "properties": {...}, "additionalProperties": false}} 

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

Unit Tests (13 new tests):
uv run pytest tests/test_llm_agent_schema_converter.py -v

  • TestIsTypedDict - TypedDict detection (4 tests)
  • TestTypedDictSchema - Schema generation for simple, nested, optional TypedDicts (5 tests)
  • TestDictWarnings - Warnings for dict and list[dict] usage (2 tests)
  • TestFunctionToolWithTypedDict - End-to-end tool schema generation (2 tests)

Integration Tests:
uv run python line/llm_agent/scripts/test_provider.py --tests nested_objects,nested_objects_fail

  • nested_objects - Validates TypedDict works with GPT 5.2, Claude, Gemini 2.5/3
  • nested_objects_fail - Validates list[dict] correctly fails with OpenAI (negative test)

Checklist

  • I have read the contributing guidelines
  • I have added tests that prove my fix is effective or that my feature works
  • I have formatted my code with make format

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.

1 participant