Skip to content

Conversation

@g-eoj
Copy link
Contributor

@g-eoj g-eoj commented Nov 17, 2025

This PR adds a method to get the json schema for agent output.

It aims to support all output types, including special ones:

  • text only
  • BinaryImage
  • DeferredToolRequests

Simple example:

agent = Agent('test')
agent.output_json_schema()
{'type': 'string'}

Override agent output types:

agent = Agent('test')
agent.output_json_schema(output_type=bool)
{'type': 'bool'}

Closes #3225

@g-eoj
Copy link
Contributor Author

g-eoj commented Nov 17, 2025

@DouweM is there a source of truth I can reference for what the schema output should be for tests? I think I will get stuck figuring this out so any guidance will be appreciated.

Edit: I added snapshots of expected output to the tests. Please review them carefully as I'm guessing what they are supposed to be.

@g-eoj g-eoj marked this pull request as ready for review November 17, 2025 21:23
@g-eoj g-eoj changed the title Add output_json_schema property to Agents Add output_json_schema method to Agent class Nov 19, 2025
@g-eoj
Copy link
Contributor Author

g-eoj commented Nov 21, 2025

@DouweM thanks for the reviews so far.

Fixed: The PR has an issue where it always adds str to the output schema if the AutoOutputSchema is selected. However I think this is due to a bug not caused by the PR. I think it is because

text_processor=processor,
will always set allows_text to True. This doesn't seem right but I'll wait for confirmation.

@g-eoj g-eoj requested a review from DouweM November 21, 2025 23:38
@g-eoj g-eoj marked this pull request as draft November 21, 2025 23:45
@g-eoj g-eoj marked this pull request as ready for review November 22, 2025 00:43
@g-eoj g-eoj marked this pull request as draft November 22, 2025 01:50
@g-eoj
Copy link
Contributor Author

g-eoj commented Nov 28, 2025

@DouweM how should duplicate tool output types be handled? For example:

output_type=[ToolOutput(bool, name='one'), ToolOutput(bool, name='two'), ToolOutput(bool, name='three')]

Can it be:

{'anyOf': [{'type': 'boolean'}, {'type': 'boolean'}, {'type': 'boolean'}]}  

Or should duplicates be removed?

@g-eoj g-eoj requested a review from DouweM November 28, 2025 18:17
@g-eoj
Copy link
Contributor Author

g-eoj commented Nov 28, 2025

@DouweM ready for review. The test failure doesn't appear to be from this PR. I have one question about whether we should remove duplicate output types: #3454 (comment)

@DouweM
Copy link
Collaborator

DouweM commented Nov 28, 2025

@g-eoj Yeah that's fine to be deduplicated. The code using agent.run(...).output wouldn't be able to tell the different bools apart either (except by inspecting the message history for tool call parts).

I'll do another review later or early next week, I have some some more PRs to get through :)

'enum': ['audio/wav', 'audio/mpeg', 'audio/ogg', 'audio/flac', 'audio/aiff', 'audio/aac'],
'type': 'string',
},
{'enum': ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], 'type': 'string'},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we really should only have this type in this case, as BinaryImage is essentially a BinaryContent with is_image guaranteed to be true. Fixing this may require tweaking of BinaryImage though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should try to tweak BinaryImage in this PR? Or make a separate issue?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@g-eoj I'd like to do it in this PR if it's not too complicated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give it a try

@DouweM DouweM changed the title Add output_json_schema method to Agent class Add Agent.output_json_schema() method Dec 1, 2025
@g-eoj
Copy link
Contributor Author

g-eoj commented Dec 1, 2025

@DouweM can you look at the approach I'm taking now? I'm not getting any info from _output_schema and instead am using Agent.output_type. Is this safe?

@g-eoj g-eoj requested a review from DouweM December 2, 2025 18:22
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@g-eoj This is so much cleaner that I'm a little embarrassed I didn't come up with it before.

@g-eoj g-eoj requested a review from DouweM December 3, 2025 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add method to get an agent's output JSON schema

2 participants