Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/agents/agent_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@ def _is_subclass_of_base_model_or_dict(t: Any) -> bool:
return issubclass(t, BaseModel | dict)


def _type_to_str(t: type[Any]) -> str:
def _type_to_str(t: Any) -> str:
origin = get_origin(t)
args = get_args(t)

if origin is None:
# It's a simple type like `str`, `int`, etc.
return t.__name__
return getattr(t, "__name__", repr(t))
elif args:
args_str = ", ".join(_type_to_str(arg) for arg in args)
return f"{origin.__name__}[{args_str}]"
origin_name = getattr(origin, "__name__", str(origin))
return f"{origin_name}[{args_str}]"
else:
return str(t)
14 changes: 13 additions & 1 deletion tests/test_output_tool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Any
from typing import Any, Literal, cast

import pytest
from pydantic import BaseModel
Expand Down Expand Up @@ -77,6 +77,18 @@ def test_structured_output_list():
assert validated == ["foo", "bar"]


def test_structured_output_literal_name_handles_literal_values():
output_schema = AgentOutputSchema(output_type=cast(type[Any], Literal["ok"]))

assert output_schema.name() == "Literal['ok']"


def test_structured_output_nested_literal_name_handles_literal_values():
output_schema = AgentOutputSchema(output_type=list[Literal["ok", "done"]])

assert output_schema.name() == "list[Literal['ok', 'done']]"


def test_structured_output_generic_dict_is_not_wrapped():
output_schema = AgentOutputSchema(output_type=dict[str, int], strict_json_schema=False)
assert output_schema.output_type == dict[str, int]
Expand Down
Loading