Skip to content

fix: #3357 AgentOutputSchema.name() crash on Literal output types#3378

Closed
john-rocky wants to merge 1 commit into
openai:mainfrom
john-rocky:fix/3357-literal-output-schema
Closed

fix: #3357 AgentOutputSchema.name() crash on Literal output types#3378
john-rocky wants to merge 1 commit into
openai:mainfrom
john-rocky:fix/3357-literal-output-schema

Conversation

@john-rocky
Copy link
Copy Markdown

Summary

AgentOutputSchema.name() previously crashed for any output type that contained typing.Literal, because _type_to_str recursed into each typing arg and unconditionally read __name__. The Literal value (e.g. the "ok" inside Literal["ok"]) is a str instance rather than a class, so the lookup raised AttributeError and 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:

  • If the recursive arg isn't a class, fall back to repr(t) so literal values keep their original form ("'ok'", '1', etc.).
  • Resolve origin names through 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_str is now typed as t: Any to match the recursive call, which already passed non-type values into itself.

Test plan

Added test_structured_output_literal_name_does_not_crash covering:

  • 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]"
  • Plain types and existing generics (str, list[int]) unchanged
$ make format
All checks passed!
$ make lint
All checks passed!
$ uv run mypy src/agents/agent_output.py tests/test_output_tool.py
Success: no issues found in 2 source files
$ uv run pytest tests/test_output_tool.py -v
...
tests/test_output_tool.py::test_structured_output_literal_name_does_not_crash PASSED
============================== 12 passed in 0.11s ==============================

Issue number

Closes #3357.

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation
  • I've run `make lint` and `make format`
  • I've made sure tests pass

`_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.
@seratch seratch added feature:core duplicate This issue or pull request already exists labels May 13, 2026
@seratch
Copy link
Copy Markdown
Member

seratch commented May 14, 2026

resolved by #3358

@seratch seratch closed this May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists feature:core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AgentOutputSchema.name() fails for Literal output types

2 participants