Skip to content

Commit 0ee01f1

Browse files
committed
Add output_json_schema property to Agents
1 parent 359c6d2 commit 0ee01f1

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

pydantic_ai_slim/pydantic_ai/_output.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pydantic_ai._instrumentation import InstrumentationNames
1616

1717
from . import _function_schema, _utils, messages as _messages
18+
from ._json_schema import JsonSchema
1819
from ._run_context import AgentDepsT, RunContext
1920
from .exceptions import ModelRetry, ToolRetryError, UserError
2021
from .output import (
@@ -226,6 +227,9 @@ def mode(self) -> OutputMode:
226227
def allows_text(self) -> bool:
227228
return self.text_processor is not None
228229

230+
def dump(self) -> JsonSchema:
231+
raise NotImplementedError()
232+
229233
@classmethod
230234
def build( # noqa: C901
231235
cls,
@@ -405,6 +409,13 @@ def __init__(
405409
def mode(self) -> OutputMode:
406410
return 'auto'
407411

412+
def dump(self) -> JsonSchema:
413+
if self.toolset:
414+
toolset_processors = [self.toolset.processors[k] for k in self.toolset.processors]
415+
processors_union = UnionOutputProcessor(toolset_processors).object_def.json_schema
416+
return processors_union
417+
return self.processor.object_def.json_schema
418+
408419

409420
@dataclass(init=False)
410421
class TextOutputSchema(OutputSchema[OutputDataT]):
@@ -425,6 +436,9 @@ def __init__(
425436
def mode(self) -> OutputMode:
426437
return 'text'
427438

439+
def dump(self) -> JsonSchema:
440+
return {'type': 'string'}
441+
428442

429443
class ImageOutputSchema(OutputSchema[OutputDataT]):
430444
def __init__(self, *, allows_deferred_tools: bool):
@@ -450,6 +464,9 @@ def __init__(
450464
)
451465
self.processor = processor
452466

467+
def dump(self) -> JsonSchema:
468+
return self.object_def.json_schema
469+
453470

454471
class NativeOutputSchema(StructuredTextOutputSchema[OutputDataT]):
455472
@property
@@ -523,6 +540,11 @@ def __init__(
523540
def mode(self) -> OutputMode:
524541
return 'tool'
525542

543+
def dump(self) -> JsonSchema:
544+
toolset_processors = [self.toolset.processors[k] for k in self.toolset.processors]
545+
processors_union = UnionOutputProcessor(toolset_processors).object_def.json_schema
546+
return processors_union
547+
526548

527549
class BaseOutputProcessor(ABC, Generic[OutputDataT]):
528550
@abstractmethod
@@ -725,7 +747,10 @@ def __init__(
725747
json_schemas: list[ObjectJsonSchema] = []
726748
self._processors = {}
727749
for output in outputs:
728-
processor = ObjectOutputProcessor(output=output, strict=strict)
750+
if isinstance(output, ObjectOutputProcessor):
751+
processor = output
752+
else:
753+
processor = ObjectOutputProcessor(output=output, strict=strict)
729754
object_def = processor.object_def
730755

731756
object_key = object_def.name or output.__name__

pydantic_ai_slim/pydantic_ai/agent/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
UserPromptNode,
3535
capture_run_messages,
3636
)
37+
from .._json_schema import JsonSchema
3738
from .._output import OutputToolset
3839
from .._tool_manager import ToolManager
3940
from ..builtin_tools import AbstractBuiltinTool
@@ -391,6 +392,11 @@ def deps_type(self) -> type:
391392
"""The type of dependencies used by the agent."""
392393
return self._deps_type
393394

395+
@property
396+
def output_json_schema(self) -> JsonSchema:
397+
"""The output JSON schema."""
398+
return self._output_schema.dump()
399+
394400
@property
395401
def output_type(self) -> OutputSpec[OutputDataT]:
396402
"""The type of data output by agent runs, used to validate the data returned by the model, defaults to `str`."""

pydantic_ai_slim/pydantic_ai/agent/abstract.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
result,
2424
usage as _usage,
2525
)
26+
from .._json_schema import JsonSchema
2627
from .._tool_manager import ToolManager
2728
from ..builtin_tools import AbstractBuiltinTool
2829
from ..output import OutputDataT, OutputSpec
@@ -101,6 +102,12 @@ def deps_type(self) -> type:
101102
"""The type of dependencies used by the agent."""
102103
raise NotImplementedError
103104

105+
@property
106+
@abstractmethod
107+
def output_json_schema(self) -> JsonSchema:
108+
"""The output JSON schema."""
109+
raise NotImplementedError
110+
104111
@property
105112
@abstractmethod
106113
def output_type(self) -> OutputSpec[OutputDataT]:

pydantic_ai_slim/pydantic_ai/agent/wrapper.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
models,
1111
usage as _usage,
1212
)
13+
from .._json_schema import JsonSchema
1314
from ..builtin_tools import AbstractBuiltinTool
1415
from ..output import OutputDataT, OutputSpec
1516
from ..run import AgentRun
@@ -45,6 +46,10 @@ def name(self) -> str | None:
4546
def name(self, value: str | None) -> None:
4647
self.wrapped.name = value
4748

49+
@property
50+
def output_json_schema(self) -> JsonSchema:
51+
return self.wrapped.output_json_schema
52+
4853
@property
4954
def deps_type(self) -> type:
5055
return self.wrapped.deps_type

0 commit comments

Comments
 (0)