66from abc import ABC , abstractmethod
77from collections .abc import Awaitable , Callable , Sequence
88from dataclasses import dataclass , field
9- from functools import cached_property
109from typing import TYPE_CHECKING , Any , Generic , Literal , cast , overload
1110
1211from pydantic import Json , TypeAdapter , ValidationError
1615from pydantic_ai ._instrumentation import InstrumentationNames
1716
1817from . import _function_schema , _utils , messages as _messages
19- from ._json_schema import JsonSchema
2018from ._run_context import AgentDepsT , RunContext
2119from .exceptions import ModelRetry , ToolRetryError , UserError
2220from .output import (
@@ -228,10 +226,6 @@ def mode(self) -> OutputMode:
228226 def allows_text (self ) -> bool :
229227 return self .text_processor is not None
230228
231- @cached_property
232- def json_schema (self ) -> JsonSchema :
233- raise NotImplementedError ()
234-
235229 @classmethod
236230 def build ( # noqa: C901
237231 cls ,
@@ -385,56 +379,6 @@ def _build_processor(
385379
386380 return UnionOutputProcessor (outputs = outputs , strict = strict , name = name , description = description )
387381
388- def build_json_schema (self ) -> JsonSchema : # noqa: C901
389- # allow any output with {'type': 'string'} if no constraints
390- if not any ([self .allows_deferred_tools , self .allows_image , self .object_def , self .toolset ]):
391- return TypeAdapter (str ).json_schema ()
392-
393- json_schemas : list [ObjectJsonSchema ] = []
394-
395- processor = getattr (self , 'processor' , None )
396- if isinstance (processor , ObjectOutputProcessor ):
397- json_schema = processor .object_def .json_schema
398- if k := processor .outer_typed_dict_key :
399- json_schema = json_schema ['properties' ][k ]
400- json_schemas .append (json_schema )
401-
402- elif self .toolset :
403- if self .allows_text :
404- json_schema = TypeAdapter (str ).json_schema ()
405- json_schemas .append (json_schema )
406- for tool_processor in self .toolset .processors .values ():
407- json_schema = tool_processor .object_def .json_schema
408- if k := tool_processor .outer_typed_dict_key :
409- json_schema = json_schema ['properties' ][k ]
410- if json_schema not in json_schemas :
411- json_schemas .append (json_schema )
412-
413- elif self .allows_text :
414- json_schema = TypeAdapter (str ).json_schema ()
415- json_schemas .append (json_schema )
416-
417- if self .allows_deferred_tools :
418- json_schema = TypeAdapter (DeferredToolRequests ).json_schema (mode = 'serialization' )
419- if json_schema not in json_schemas :
420- json_schemas .append (json_schema )
421-
422- if self .allows_image :
423- json_schema = TypeAdapter (_messages .BinaryImage ).json_schema ()
424- json_schema = {k : v for k , v in json_schema ['properties' ].items () if k in ['data' , 'media_type' ]}
425- if json_schema not in json_schemas :
426- json_schemas .append (json_schema )
427-
428- if len (json_schemas ) == 1 :
429- return json_schemas [0 ]
430-
431- json_schemas , all_defs = _utils .merge_json_schema_defs (json_schemas )
432- json_schema : JsonSchema = {'anyOf' : json_schemas }
433- if all_defs :
434- json_schema ['$defs' ] = all_defs
435-
436- return json_schema
437-
438382
439383@dataclass (init = False )
440384class AutoOutputSchema (OutputSchema [OutputDataT ]):
@@ -463,10 +407,6 @@ def __init__(
463407 def mode (self ) -> OutputMode :
464408 return 'auto'
465409
466- @cached_property
467- def json_schema (self ) -> JsonSchema :
468- return self .build_json_schema ()
469-
470410
471411@dataclass (init = False )
472412class TextOutputSchema (OutputSchema [OutputDataT ]):
@@ -487,10 +427,6 @@ def __init__(
487427 def mode (self ) -> OutputMode :
488428 return 'text'
489429
490- @cached_property
491- def json_schema (self ) -> JsonSchema :
492- return self .build_json_schema ()
493-
494430
495431class ImageOutputSchema (OutputSchema [OutputDataT ]):
496432 def __init__ (self , * , allows_deferred_tools : bool ):
@@ -500,10 +436,6 @@ def __init__(self, *, allows_deferred_tools: bool):
500436 def mode (self ) -> OutputMode :
501437 return 'image'
502438
503- @cached_property
504- def json_schema (self ) -> JsonSchema :
505- return self .build_json_schema ()
506-
507439
508440@dataclass (init = False )
509441class StructuredTextOutputSchema (OutputSchema [OutputDataT ], ABC ):
@@ -520,10 +452,6 @@ def __init__(
520452 )
521453 self .processor = processor
522454
523- @cached_property
524- def json_schema (self ) -> JsonSchema :
525- return self .build_json_schema ()
526-
527455
528456class NativeOutputSchema (StructuredTextOutputSchema [OutputDataT ]):
529457 @property
@@ -590,10 +518,6 @@ def __init__(
590518 def mode (self ) -> OutputMode :
591519 return 'tool'
592520
593- @cached_property
594- def json_schema (self ) -> JsonSchema :
595- return self .build_json_schema ()
596-
597521
598522class BaseOutputProcessor (ABC , Generic [OutputDataT ]):
599523 @abstractmethod
0 commit comments