Skip to content

Add support for schema_generator to ToolOutput #1535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions pydantic_ai_slim/pydantic_ai/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, Callable, Generic, Literal, Union, cast

from pydantic import TypeAdapter, ValidationError
from pydantic.json_schema import GenerateJsonSchema
from typing_extensions import TypedDict, TypeVar, get_args, get_origin
from typing_inspection import typing_objects
from typing_inspection.introspection import is_union_origin
Expand Down Expand Up @@ -104,8 +105,10 @@ def build(
description = output_type.description
output_type_ = output_type.output_type
strict = output_type.strict
schema_generator = output_type.schema_generator
else:
output_type_ = output_type
schema_generator = GenerateToolJsonSchema

if output_type_option := extract_str_from_union(output_type):
output_type_ = output_type_option.value
Expand All @@ -122,15 +125,25 @@ def build(
tools[tool_name] = cast(
OutputSchemaTool[T],
OutputSchemaTool(
output_type=arg, name=tool_name, description=description, multiple=True, strict=strict
output_type=arg,
name=tool_name,
description=description,
multiple=True,
strict=strict,
schema_generator=schema_generator,
),
)
else:
name = name or DEFAULT_OUTPUT_TOOL_NAME
tools[name] = cast(
OutputSchemaTool[T],
OutputSchemaTool(
output_type=output_type_, name=name, description=description, multiple=False, strict=strict
output_type=output_type_,
name=name,
description=description,
multiple=False,
strict=strict,
schema_generator=schema_generator,
),
)

Expand Down Expand Up @@ -173,15 +186,22 @@ class OutputSchemaTool(Generic[OutputDataT]):
type_adapter: TypeAdapter[Any]

def __init__(
self, *, output_type: type[OutputDataT], name: str, description: str | None, multiple: bool, strict: bool | None
self,
*,
output_type: type[OutputDataT],
name: str,
description: str | None,
multiple: bool,
strict: bool | None,
schema_generator: type[GenerateJsonSchema],
):
"""Build a OutputSchemaTool from a response type."""
if _utils.is_model_like(output_type):
self.type_adapter = TypeAdapter(output_type)
outer_typed_dict_key: str | None = None
# noinspection PyArgumentList
parameters_json_schema = _utils.check_object_json_schema(
self.type_adapter.json_schema(schema_generator=GenerateToolJsonSchema)
self.type_adapter.json_schema(schema_generator=schema_generator)
)
else:
response_data_typed_dict = TypedDict( # noqa: UP013
Expand All @@ -192,7 +212,7 @@ def __init__(
outer_typed_dict_key = 'response'
# noinspection PyArgumentList
parameters_json_schema = _utils.check_object_json_schema(
self.type_adapter.json_schema(schema_generator=GenerateToolJsonSchema)
self.type_adapter.json_schema(schema_generator=schema_generator)
)
# including `response_data_typed_dict` as a title here doesn't add anything and could confuse the LLM
parameters_json_schema.pop('title')
Expand Down
5 changes: 4 additions & 1 deletion pydantic_ai_slim/pydantic_ai/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from datetime import datetime
from typing import TYPE_CHECKING, Generic, Union, cast

from pydantic.json_schema import GenerateJsonSchema
from typing_extensions import TypeVar, assert_type, deprecated, overload

from . import _utils, exceptions, messages as _messages, models
from .messages import AgentStreamEvent, FinalResultEvent
from .tools import AgentDepsT, RunContext
from .tools import AgentDepsT, GenerateToolJsonSchema, RunContext
from .usage import Usage, UsageLimits

if TYPE_CHECKING:
Expand Down Expand Up @@ -76,12 +77,14 @@ def __init__(
description: str | None = None,
max_retries: int | None = None,
strict: bool | None = None,
schema_generator: type[GenerateJsonSchema] = GenerateToolJsonSchema,
):
self.output_type = type_
self.name = name
self.description = description
self.max_retries = max_retries
self.strict = strict
self.schema_generator = schema_generator

# TODO: add support for call and make type_ optional, with the following logic:
# if type_ is None and call is None:
Expand Down