fix(llm): handle TypedDict required and optional fields in response format - #6546
Open
hsusul wants to merge 1 commit into
Open
fix(llm): handle TypedDict required and optional fields in response format#6546hsusul wants to merge 1 commit into
hsusul wants to merge 1 commit into
Conversation
…ormat When converting a TypedDict to a Pydantic BaseModel via to_response_format_param, raw type annotations were passed directly to create_model, causing PydanticUserError/PydanticForbiddenQualifier when Required[...] or NotRequired[...] qualifiers or string annotations were present. Additionally, all fields were marked as required (...) regardless of NotRequired annotations or total=False. This fix: 1. Uses get_type_hints and unwraps Required/NotRequired qualifiers from field type annotations. 2. Inspects __required_keys__, __optional_keys__, __total__, and annotation markers to accurately determine field requirement status. 3. Adds unit test coverage for TypedDict response format parameters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a bug in
to_response_format_paramwhere passing aTypedDictcontainingRequired[...]orNotRequired[...]qualifiers (or stringified type annotations underfrom __future__ import annotations) raisesPydanticUserErrororPydanticForbiddenQualifierwhen converted to a PydanticBaseModel.Additionally, fixes an issue where all
TypedDictfields were previously set as required (...) regardless ofNotRequiredannotations ortotal=False.Changes
_get_typed_dict_hintsand_clean_type_annotationto safely unwrapRequiredandNotRequiredqualifiers and evaluate type annotations._is_typed_dict_field_requiredto check__required_keys__,__optional_keys__,__total__, and annotation markers (NotRequired/Required) so optional fields are properly initialized withdefault=None.test_to_response_format_param_typed_dicttotests/test_llm_utils.pyverifying response format parameter generation and OpenAI response format conversion forTypedDict.Verification
uv run pytest tests/test_llm_utils.pypasses (14/14 tests).make lint(ruff check) passes with zero issues.make type-check(mypy) passes across 625 source files with zero issues.