fix: #3357 AgentOutputSchema.name() crash on Literal output types#3378
Closed
john-rocky wants to merge 1 commit into
Closed
fix: #3357 AgentOutputSchema.name() crash on Literal output types#3378john-rocky wants to merge 1 commit into
john-rocky wants to merge 1 commit into
Conversation
`_type_to_str` unconditionally read `__name__` on each typing arg, which raised `AttributeError` for `Literal["ok"]` because the literal value is a `str` instance rather than a class. The schema itself built fine but `name()` (and therefore the agent trace path) crashed when starting a run with a structured output type that contained any literal. Fall back to `repr()` for non-type args and to `_name`/`str(origin)` for typing constructs that lack `__name__`. Adds a focused regression test for `Literal["ok"]`, multi-member `Literal`, nested `list[Literal[...]]`, and non-string `Literal[1, 2]` cases. Fixes openai#3357.
Member
|
resolved by #3358 |
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
AgentOutputSchema.name()previously crashed for any output type that containedtyping.Literal, because_type_to_strrecursed into each typing arg and unconditionally read__name__. The Literal value (e.g. the"ok"insideLiteral["ok"]) is astrinstance rather than a class, so the lookup raisedAttributeErrorand broke the trace path when the run started.schema.json_schema()already worked, so the bug only surfaced at the very last step of agent setup.The fix is local to
_type_to_str:repr(t)so literal values keep their original form ("'ok'",'1', etc.).getattr(origin, "__name__", None) or getattr(origin, "_name", None) or str(origin)so typing constructs that only expose_name(typing.Literal,typing.Union) still produce a readable label._type_to_stris now typed ast: Anyto match the recursive call, which already passed non-type values into itself.Test plan
Added
test_structured_output_literal_name_does_not_crashcovering:Literal["ok"]→"Literal['ok']"(the exact repro from the issue)Literal["ok", "done"]→"Literal['ok', 'done']"list[Literal["ok", "done"]]→"list[Literal['ok', 'done']]"Literal[1, 2]→"Literal[1, 2]"str,list[int]) unchangedIssue number
Closes #3357.
Checks