@@ -390,23 +390,28 @@ def build_json_schema(self) -> JsonSchema:
390390 if not any ([self .allows_deferred_tools , self .allows_image , self .object_def , self .toolset ]):
391391 return TypeAdapter (str ).json_schema ()
392392
393- object_keys : list [str ] = []
394393 json_schemas : list [ObjectJsonSchema ] = []
395394
396- if self .object_def :
397- json_schema = self .object_def .json_schema
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 ]
398400 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+
401402 elif self .toolset :
402- for name , tool_processor in self .toolset .processors .items ():
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 ():
403407 json_schema = tool_processor .object_def .json_schema
408+ if k := tool_processor .outer_typed_dict_key :
409+ json_schema = json_schema ['properties' ][k ]
404410 json_schemas .append (json_schema )
405- object_keys . append ( name )
406- elif self .text_processor :
411+
412+ elif self .allows_text :
407413 json_schema = TypeAdapter (str ).json_schema ()
408414 json_schemas .append (json_schema )
409- object_keys .append (str .__name__ )
410415
411416 special_output_types : list [type ] = []
412417 if self .allows_deferred_tools :
@@ -416,24 +421,16 @@ def build_json_schema(self) -> JsonSchema:
416421 for output_type in special_output_types :
417422 output_type_json_schema = TypeAdapter (output_type ).json_schema (mode = 'serialization' )
418423 json_schemas .append (output_type_json_schema )
419- object_keys .append (output_type .__name__ )
420424
421- # do not further process JSON if not needed
422425 if len (json_schemas ) == 1 :
423426 return json_schemas [0 ]
424427
425428 json_schemas , all_defs = _utils .merge_json_schema_defs (json_schemas )
429+ json_schema : JsonSchema = {'anyOf' : json_schemas }
430+ if all_defs :
431+ json_schema ['$defs' ] = all_defs
426432
427- unique_object_keys : list [str ] = []
428- for key in object_keys :
429- count = 1
430- new_key = key
431- while new_key in unique_object_keys :
432- count += 1
433- new_key = f'{ key } _{ count } '
434- unique_object_keys .append (new_key )
435-
436- return UnionOutputProcessor .make_discriminated_json_schema_union (unique_object_keys , json_schemas , all_defs )
433+ return json_schema
437434
438435
439436@dataclass (init = False )
0 commit comments