Skip to content

Conversation

@NicolasPllr1
Copy link
Contributor

@NicolasPllr1 NicolasPllr1 commented Nov 17, 2025

This PR adds a new validation_context parameter to the Agent.

This object is either Any or a callable on the agent's run context returning Any. It is fed to the Pydantic validation of all outputs as additional context

Related issue

Add tests involving the new 'validation context' for:

- Pydantic model as the output type
- Tool, native and prompted output
- Tool calling
- Output function
- Output validator
@NicolasPllr1 NicolasPllr1 marked this pull request as ready for review November 17, 2025 08:16
try:
self._next_node = await self._handle_text_response(ctx, text, text_processor)
self._next_node = await self._handle_text_response(
ctx, ctx.deps.tool_manager.validation_ctx, text, text_processor
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we already pass in ctx, we don't need a new arg do we?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it make sense to store the validation context directly on ctx.deps, instead of going through the tool manager?

self,
data: str,
run_context: RunContext[AgentDepsT],
validation_context: Any | None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's have a default of None

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also while we're at it, let's require kwargs for everything after data, by inserting , *,

def validate(
self,
data: str | dict[str, Any] | None,
allow_partial: bool = False,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as up, let's require kwargs for everything but data

model_settings: Optional model request settings to use for this agent's runs, by default.
retries: The default number of retries to allow for tool calls and output validation, before raising an error.
For model request retries, see the [HTTP Request Retries](../retries.md) documentation.
validation_context: Additional validation context used to validate all outputs.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's link to the Pydantic doc

result_data = await text_processor.process(text, run_context, validation_context)

for validator in ctx.deps.output_validators:
result_data = await validator.validate(result_data, run_context)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally, output validators would have access to the validation context as well, as they could call model_validate themselves. They already have access to RunContext, so maybe it'd make sense to store the validation context on there? As the validation context callable itself needs RunContext, building run context could look like ctx = <run context>; validation_ctx = callable(ctx); ctx = replace(ctx, validation_ctx = validation_ctx)

I think that'd be a refactor worth exploring that could allow us to drop a lot of the new arguments.

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.

Passing Pydantic validation context for output and tool arguments validation

2 participants