@@ -385,44 +385,38 @@ def _build_processor(
385385
386386 return UnionOutputProcessor (outputs = outputs , strict = strict , name = name , description = description )
387387
388- @staticmethod
389- def build_json_schema (
390- allows_deferred_tools : bool = False ,
391- allows_image : bool = False ,
392- allows_text : bool = False ,
393- base_processor : BaseObjectOutputProcessor [OutputDataT ] | None = None ,
394- toolset_processors : dict [str , ObjectOutputProcessor [OutputDataT ]] | None = None ,
395- ) -> JsonSchema :
388+ def build_json_schema (self ) -> JsonSchema :
396389 # allow any output with {'type': 'string'} if no constraints
397- if not any ([allows_deferred_tools , allows_image , base_processor , toolset_processors ]):
390+ if not any ([self . allows_deferred_tools , self . allows_image , self . object_def , self . toolset ]):
398391 return TypeAdapter (str ).json_schema ()
399392
400393 object_keys : list [str ] = []
401394 json_schemas : list [ObjectJsonSchema ] = []
402395
403- if toolset_processors :
404- for name , tool_processor in toolset_processors .items ():
405- json_schema = tool_processor .object_def .json_schema .copy ()
396+ if self .object_def :
397+ json_schema = self .object_def .json_schema
398+ json_schemas .append (json_schema )
399+ object_key = json_schema .get ('title' ) or self .object_def .name or 'result'
400+ object_keys .append (object_key )
401+ elif self .toolset :
402+ for name , tool_processor in self .toolset .processors .items ():
403+ json_schema = tool_processor .object_def .json_schema
406404 json_schemas .append (json_schema )
407405 object_keys .append (name )
408-
409- if base_processor :
410- json_schema = base_processor .object_def .json_schema .copy ()
406+ elif self .text_processor :
407+ json_schema = TypeAdapter (str ).json_schema ()
411408 json_schemas .append (json_schema )
412- object_keys .append (json_schema . get ( 'title' , 'result' ) )
409+ object_keys .append (str . __name__ )
413410
414411 special_output_types : list [type ] = []
415- if allows_text :
416- special_output_types .append (str )
417- if allows_deferred_tools :
412+ if self .allows_deferred_tools :
418413 special_output_types .append (DeferredToolRequests )
419- if allows_image :
414+ if self . allows_image :
420415 special_output_types .append (_messages .BinaryImage )
421- if special_output_types :
422- for output_type in special_output_types :
423- output_type_json_schema = TypeAdapter (output_type ).json_schema (mode = 'serialization' )
424- json_schemas .append (output_type_json_schema )
425- object_keys .append (output_type .__name__ )
416+ for output_type in special_output_types :
417+ output_type_json_schema = TypeAdapter (output_type ).json_schema (mode = 'serialization' )
418+ json_schemas .append (output_type_json_schema )
419+ object_keys .append (output_type .__name__ )
426420
427421 # do not further process JSON if not needed
428422 if len (json_schemas ) == 1 :
@@ -471,11 +465,7 @@ def mode(self) -> OutputMode:
471465
472466 @cached_property
473467 def json_schema (self ) -> JsonSchema :
474- return OutputSchema [OutputDataT ].build_json_schema (
475- base_processor = self .processor ,
476- allows_deferred_tools = self .allows_deferred_tools ,
477- allows_image = self .allows_image ,
478- )
468+ return self .build_json_schema ()
479469
480470
481471@dataclass (init = False )
@@ -499,9 +489,7 @@ def mode(self) -> OutputMode:
499489
500490 @cached_property
501491 def json_schema (self ) -> JsonSchema :
502- return OutputSchema [OutputDataT ].build_json_schema (
503- allows_deferred_tools = self .allows_deferred_tools , allows_image = self .allows_image , allows_text = True
504- )
492+ return self .build_json_schema ()
505493
506494
507495class ImageOutputSchema (OutputSchema [OutputDataT ]):
@@ -514,9 +502,7 @@ def mode(self) -> OutputMode:
514502
515503 @cached_property
516504 def json_schema (self ) -> JsonSchema :
517- return OutputSchema [OutputDataT ].build_json_schema (
518- allows_deferred_tools = self .allows_deferred_tools , allows_image = True
519- )
505+ return self .build_json_schema ()
520506
521507
522508@dataclass (init = False )
@@ -536,11 +522,7 @@ def __init__(
536522
537523 @cached_property
538524 def json_schema (self ) -> JsonSchema :
539- return OutputSchema [OutputDataT ].build_json_schema (
540- base_processor = self .processor ,
541- allows_deferred_tools = self .allows_deferred_tools ,
542- allows_image = self .allows_image ,
543- )
525+ return self .build_json_schema ()
544526
545527
546528class NativeOutputSchema (StructuredTextOutputSchema [OutputDataT ]):
@@ -610,12 +592,7 @@ def mode(self) -> OutputMode:
610592
611593 @cached_property
612594 def json_schema (self ) -> JsonSchema :
613- return OutputSchema [OutputDataT ].build_json_schema (
614- toolset_processors = self .toolset .processors , # pyright: ignore[reportOptionalMemberAccess]
615- allows_deferred_tools = self .allows_deferred_tools ,
616- allows_image = self .allows_image ,
617- allows_text = self .allows_text ,
618- )
595+ return self .build_json_schema ()
619596
620597
621598class BaseOutputProcessor (ABC , Generic [OutputDataT ]):
0 commit comments