Skip to content

Commit d78106b

Browse files
committed
Handle TextOutput
1 parent 85e929f commit d78106b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

pydantic_ai_slim/pydantic_ai/agent/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,8 @@ def output_json_schema(self, output_type: OutputSpec[OutputDataT | RunOutputData
984984
function_schema = _function_schema.function_schema(_, GenerateToolJsonSchema)
985985
json_schema = function_schema.json_schema
986986
json_schema['description'] = function_schema.description
987+
elif isinstance(_, _output.TextOutput):
988+
json_schema = TypeAdapter(str).json_schema(mode='serialization')
987989
else:
988990
json_schema = TypeAdapter(_).json_schema(mode='serialization')
989991

tests/test_agent_output_schemas.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
NativeOutput,
1010
PromptedOutput,
1111
StructuredDict,
12+
TextOutput,
1213
ToolOutput,
1314
)
1415

@@ -28,6 +29,28 @@ async def test_text_output_json_schema():
2829
agent = Agent('test')
2930
assert agent.output_json_schema() == snapshot({'type': 'string'})
3031

32+
def func(x: str) -> str:
33+
return x
34+
35+
agent = Agent('test', output_type=TextOutput(func))
36+
assert agent.output_json_schema() == snapshot({'type': 'string'})
37+
38+
39+
async def test_function_output_json_schema():
40+
def func(x: int) -> int:
41+
return x
42+
43+
agent = Agent('test', output_type=[func])
44+
assert agent.output_json_schema() == snapshot(
45+
{
46+
'additionalProperties': False,
47+
'properties': {'x': {'type': 'integer'}},
48+
'required': ['x'],
49+
'type': 'object',
50+
'description': None,
51+
}
52+
)
53+
3154

3255
async def test_auto_output_json_schema():
3356
# one output

0 commit comments

Comments
 (0)