Skip to content

Commit 2782c55

Browse files
committed
Refactor
1 parent 5044fe4 commit 2782c55

File tree

1 file changed

+20
-42
lines changed

1 file changed

+20
-42
lines changed

pydantic_ai_slim/pydantic_ai/_output.py

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -385,38 +385,33 @@ 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)
421416
if special_output_types:
422417
for output_type in special_output_types:
@@ -471,11 +466,7 @@ def mode(self) -> OutputMode:
471466

472467
@cached_property
473468
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-
)
469+
return self.build_json_schema()
479470

480471

481472
@dataclass(init=False)
@@ -499,9 +490,7 @@ def mode(self) -> OutputMode:
499490

500491
@cached_property
501492
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-
)
493+
return self.build_json_schema()
505494

506495

507496
class ImageOutputSchema(OutputSchema[OutputDataT]):
@@ -514,9 +503,7 @@ def mode(self) -> OutputMode:
514503

515504
@cached_property
516505
def json_schema(self) -> JsonSchema:
517-
return OutputSchema[OutputDataT].build_json_schema(
518-
allows_deferred_tools=self.allows_deferred_tools, allows_image=True
519-
)
506+
return self.build_json_schema()
520507

521508

522509
@dataclass(init=False)
@@ -536,11 +523,7 @@ def __init__(
536523

537524
@cached_property
538525
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-
)
526+
return self.build_json_schema()
544527

545528

546529
class NativeOutputSchema(StructuredTextOutputSchema[OutputDataT]):
@@ -610,12 +593,7 @@ def mode(self) -> OutputMode:
610593

611594
@cached_property
612595
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-
)
596+
return self.build_json_schema()
619597

620598

621599
class BaseOutputProcessor(ABC, Generic[OutputDataT]):

0 commit comments

Comments
 (0)