- Notifications
You must be signed in to change notification settings - Fork 1.5k
Add Agent.output_json_schema() method #3454
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
Merged
Merged
Changes from 43 commits
Commits
Show all changes
54 commits Select commit Hold shift + click to select a range
0ee01f1 Add output_json_schema property to Agents
g-eoj 80dd6e3 Add test outline
g-eoj 7127de6 Fix typecheck errors
g-eoj 291fa1f Merge branch 'main' into 3225
g-eoj d1b2399 Capture title and desc for tool output
g-eoj 4454583 Add tests
g-eoj fdc8820 Fix typecheck errors
g-eoj 93de392 Merge branch 'main' into 3225
g-eoj 79253f1 Change to method
g-eoj ba9433f Merge branch 'main' into 3225
g-eoj 09184e2 More test stuff
g-eoj 0d58762 Improve test coverage
g-eoj 0043115 Address feedback
g-eoj 123eabc Merge branch 'main' into 3225
g-eoj 66f26b5 Fix native output
g-eoj 74ac9b6 Split test file and add more tests
g-eoj c0bc7fd Refactor
g-eoj cff87ad Skip deferred requests test
g-eoj c44c9c7 Small fixes and more tests
g-eoj 1530488 Merge branch 'main' into 3225
g-eoj 00c6ddd Update deferred requests snapshot
g-eoj 30c7b54 Maybe fix test coverage
g-eoj 37f9bed Simplify code
g-eoj 4e8509f Make copy of processor json schema
g-eoj 74ec324 Use tool_defs instead of instead of processors
g-eoj b0b796f Merge branch 'main' into 3225
DouweM 3be8f95 Revert "Use tool_defs instead of instead of processors"
g-eoj b92da23 Address feedback
g-eoj 5044fe4 Clean up tests
g-eoj 355573d Refactor
g-eoj d0e1e9c Do not discriminate
g-eoj f561373 Only include the data and media_type keys for BinaryImage
g-eoj dad1e17 Revert unneeded changes
g-eoj 597d299 Add test
g-eoj 0b55d53 Merge branch 'main' into 3225
g-eoj a0e8b24 No duplicate schemas
g-eoj 64b61d1 Use Agent.output_types to construct JSON schema
g-eoj cedeb8e Small fixes
g-eoj 4e305d7 Don't modify _output.py
g-eoj 85e929f Merge branch 'main' into 3225
g-eoj d78106b Handle TextOutput
g-eoj 1fd144b Fix function output
g-eoj 5283df9 Maybe fix coverage
g-eoj e6bc181 Small refactor to address comments
g-eoj e2415af Refactor for clarity
g-eoj 0110d47 Fix BinaryImage
g-eoj b125e1b Better solution for BinaryImage custom JSON schema
g-eoj 6ede66f Merge branch 'main' into 3225
g-eoj 604c681 Fix coverage
g-eoj 199afe7 Revert changes to BinaryImage
g-eoj 14a198f Refactor based on comments
g-eoj 3f3b695 Merge branch 'main' into 3225
g-eoj 47c5ea6 Revert changes to agent init
g-eoj 92c6b3c Don't use _flatten_output_spec
g-eoj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -11,6 +11,7 @@ | |
| from typing import TYPE_CHECKING, Any, ClassVar, overload | ||
| | ||
| from opentelemetry.trace import NoOpTracer, use_span | ||
| from pydantic import TypeAdapter | ||
| from pydantic.json_schema import GenerateJsonSchema | ||
| from typing_extensions import Self, TypeVar, deprecated | ||
| | ||
| | @@ -34,7 +35,8 @@ | |
| UserPromptNode, | ||
| capture_run_messages, | ||
| ) | ||
| from .._output import OutputToolset | ||
| from .._json_schema import JsonSchema | ||
| from .._output import OutputToolset, _flatten_output_spec # pyright: ignore[reportPrivateUsage] | ||
| from .._tool_manager import ToolManager | ||
| from ..builtin_tools import AbstractBuiltinTool | ||
| from ..models.instrumented import InstrumentationSettings, InstrumentedModel, instrument_model | ||
| | @@ -955,6 +957,47 @@ def decorator( | |
| self._system_prompt_functions.append(_system_prompt.SystemPromptRunner[AgentDepsT](func, dynamic=dynamic)) | ||
| return func | ||
| | ||
| def output_json_schema(self, output_type: OutputSpec[OutputDataT | RunOutputDataT] | None = None) -> JsonSchema: | ||
g-eoj marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| """The output JSON schema.""" | ||
| if output_type is None: | ||
| output_type = self.output_type | ||
| | ||
| # call this first to force output_type to an iterable | ||
| output_type = list(_flatten_output_spec(output_type)) | ||
| | ||
| # flatten special outputs | ||
| for i, _ in enumerate(output_type): | ||
| if isinstance(_, _output.NativeOutput): | ||
g-eoj marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| output_type[i] = _flatten_output_spec(_.outputs) | ||
| if isinstance(_, _output.PromptedOutput): | ||
| output_type[i] = _flatten_output_spec(_.outputs) | ||
| if isinstance(_, _output.ToolOutput): | ||
| output_type[i] = _flatten_output_spec(_.output) | ||
| | ||
| # final flattening | ||
| output_type = _flatten_output_spec(output_type) | ||
| | ||
| json_schemas: list[JsonSchema] = [] | ||
| for _ in output_type: | ||
| if inspect.isfunction(_) or inspect.ismethod(_): | ||
g-eoj marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| json_schema = TypeAdapter(inspect.signature(_).return_annotation).json_schema(mode='serialization') | ||
| elif isinstance(_, _output.TextOutput): | ||
g-eoj marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| json_schema = TypeAdapter(str).json_schema(mode='serialization') | ||
| else: | ||
| json_schema = TypeAdapter(_).json_schema(mode='serialization') | ||
| | ||
| if json_schema not in json_schemas: | ||
| ||
| json_schemas.append(json_schema) | ||
| | ||
| if len(json_schemas) == 1: | ||
| return json_schemas[0] | ||
| else: | ||
| json_schemas, all_defs = _utils.merge_json_schema_defs(json_schemas) | ||
| json_schema: JsonSchema = {'anyOf': json_schemas} | ||
| if all_defs: | ||
| json_schema['$defs'] = all_defs | ||
| return json_schema | ||
| | ||
| @overload | ||
| def output_validator( | ||
| self, func: Callable[[RunContext[AgentDepsT], OutputDataT], OutputDataT], / | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.