Skip to content

Commit 79253f1

Browse files
committed
Change to method
1 parent 93de392 commit 79253f1

File tree

5 files changed

+60
-21
lines changed

5 files changed

+60
-21
lines changed

pydantic_ai_slim/pydantic_ai/_output.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def mode(self) -> OutputMode:
228228
def allows_text(self) -> bool:
229229
return self.text_processor is not None
230230

231+
@abstractmethod
231232
def dump(self) -> JsonSchema:
232233
raise NotImplementedError()
233234

@@ -453,6 +454,9 @@ def __init__(self, *, allows_deferred_tools: bool):
453454
def mode(self) -> OutputMode:
454455
return 'image'
455456

457+
def dump(self) -> JsonSchema:
458+
raise NotImplementedError()
459+
456460

457461
@dataclass(init=False)
458462
class StructuredTextOutputSchema(OutputSchema[OutputDataT], ABC):

pydantic_ai_slim/pydantic_ai/agent/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,6 @@ def deps_type(self) -> type:
392392
"""The type of dependencies used by the agent."""
393393
return self._deps_type
394394

395-
@property
396-
def output_json_schema(self) -> JsonSchema:
397-
"""The output JSON schema."""
398-
return self._output_schema.dump()
399-
400395
@property
401396
def output_type(self) -> OutputSpec[OutputDataT]:
402397
"""The type of data output by agent runs, used to validate the data returned by the model, defaults to `str`."""
@@ -953,6 +948,11 @@ def decorator(
953948
self._system_prompt_functions.append(_system_prompt.SystemPromptRunner[AgentDepsT](func, dynamic=dynamic))
954949
return func
955950

951+
def output_json_schema(self, output_type: OutputSpec[OutputDataT] | None = None) -> JsonSchema:
952+
"""The output JSON schema."""
953+
output_schema = self._prepare_output_schema(output_type)
954+
return output_schema.dump()
955+
956956
@overload
957957
def output_validator(
958958
self, func: Callable[[RunContext[AgentDepsT], OutputDataT], OutputDataT], /

pydantic_ai_slim/pydantic_ai/agent/abstract.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ def deps_type(self) -> type:
102102
"""The type of dependencies used by the agent."""
103103
raise NotImplementedError
104104

105-
@property
106-
@abstractmethod
107-
def output_json_schema(self) -> JsonSchema:
108-
"""The output JSON schema."""
109-
raise NotImplementedError
110-
111105
@property
112106
@abstractmethod
113107
def output_type(self) -> OutputSpec[OutputDataT]:
@@ -129,6 +123,11 @@ def toolsets(self) -> Sequence[AbstractToolset[AgentDepsT]]:
129123
"""
130124
raise NotImplementedError
131125

126+
@abstractmethod
127+
def output_json_schema(self, output_type: OutputSpec[OutputDataT] | None = None) -> JsonSchema:
128+
"""The output JSON schema."""
129+
raise NotImplementedError
130+
132131
@overload
133132
async def run(
134133
self,

pydantic_ai_slim/pydantic_ai/agent/wrapper.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ def name(self) -> str | None:
4646
def name(self, value: str | None) -> None:
4747
self.wrapped.name = value
4848

49-
@property
50-
def output_json_schema(self) -> JsonSchema:
51-
return self.wrapped.output_json_schema
52-
5349
@property
5450
def deps_type(self) -> type:
5551
return self.wrapped.deps_type
@@ -72,6 +68,9 @@ async def __aenter__(self) -> AbstractAgent[AgentDepsT, OutputDataT]:
7268
async def __aexit__(self, *args: Any) -> bool | None:
7369
return await self.wrapped.__aexit__(*args)
7470

71+
def output_json_schema(self, output_type: OutputSpec[OutputDataT] | None = None) -> JsonSchema:
72+
return self.wrapped.output_json_schema(output_type=output_type)
73+
7574
@overload
7675
def iter(
7776
self,

tests/test_agent.py

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6132,7 +6132,7 @@ def llm(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
61326132
return ModelResponse(parts=[TextPart('')])
61336133

61346134
agent = Agent(FunctionModel(llm))
6135-
assert agent.output_json_schema == snapshot({'type': 'string'})
6135+
assert agent.output_json_schema() == snapshot({'type': 'string'})
61366136

61376137

61386138
async def test_tool_output_json_schema():
@@ -6143,7 +6143,7 @@ def llm(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
61436143
FunctionModel(llm),
61446144
output_type=[ToolOutput(bool, name='alice', description='Dreaming...')],
61456145
)
6146-
assert agent.output_json_schema == snapshot(
6146+
assert agent.output_json_schema() == snapshot(
61476147
{
61486148
'type': 'object',
61496149
'properties': {
@@ -6176,7 +6176,7 @@ def llm(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
61766176
FunctionModel(llm),
61776177
output_type=[ToolOutput(bool, name='alice'), ToolOutput(bool, name='bob')],
61786178
)
6179-
assert agent.output_json_schema == snapshot(
6179+
assert agent.output_json_schema() == snapshot(
61806180
{
61816181
'type': 'object',
61826182
'properties': {
@@ -6229,7 +6229,7 @@ def llm(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
62296229
FunctionModel(llm),
62306230
output_type=NativeOutput([bool], name='native_output_name', description='native_output_description'),
62316231
)
6232-
assert agent.output_json_schema == snapshot(
6232+
assert agent.output_json_schema() == snapshot(
62336233
{'properties': {'response': {'type': 'boolean'}}, 'required': ['response'], 'type': 'object'}
62346234
)
62356235

@@ -6242,7 +6242,7 @@ def llm(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
62426242
FunctionModel(llm),
62436243
output_type=PromptedOutput([bool], name='prompted_output_name', description='prompted_output_description'),
62446244
)
6245-
assert agent.output_json_schema == snapshot(
6245+
assert agent.output_json_schema() == snapshot(
62466246
{'properties': {'response': {'type': 'boolean'}}, 'required': ['response'], 'type': 'object'}
62476247
)
62486248

@@ -6261,7 +6261,7 @@ def llm(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
62616261
description='A human with a name and age',
62626262
)
62636263
agent = Agent(FunctionModel(llm), output_type=HumanDict)
6264-
assert agent.output_json_schema == snapshot(
6264+
assert agent.output_json_schema() == snapshot(
62656265
{
62666266
'type': 'object',
62676267
'properties': {
@@ -6289,3 +6289,40 @@ def llm(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
62896289
'additionalProperties': False,
62906290
}
62916291
)
6292+
6293+
6294+
async def test_override_output_json_schema():
6295+
def llm(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
6296+
return ModelResponse(parts=[TextPart('')])
6297+
6298+
agent = Agent(FunctionModel(llm))
6299+
assert agent.output_json_schema() == snapshot({'type': 'string'})
6300+
output_type = ([ToolOutput(bool, name='alice', description='Dreaming...')],)
6301+
assert agent.output_json_schema(output_type=output_type) == snapshot(
6302+
{
6303+
'type': 'object',
6304+
'properties': {
6305+
'result': {
6306+
'anyOf': [
6307+
{
6308+
'type': 'object',
6309+
'properties': {
6310+
'kind': {'type': 'string', 'const': 'alice'},
6311+
'data': {
6312+
'properties': {'response': {'type': 'boolean'}},
6313+
'required': ['response'],
6314+
'type': 'object',
6315+
},
6316+
},
6317+
'required': ['kind', 'data'],
6318+
'additionalProperties': False,
6319+
'title': 'alice',
6320+
'description': 'Dreaming...',
6321+
}
6322+
]
6323+
}
6324+
},
6325+
'required': ['result'],
6326+
'additionalProperties': False,
6327+
}
6328+
)

0 commit comments

Comments
 (0)