Skip to content

Commit afff2b8

Browse files
committed
only support partial on validators that take ctx
1 parent 90fd98e commit afff2b8

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

docs/output.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,7 @@ _(This example is complete, it can be run "as is")_
472472

473473
#### Partial validation during streaming {#partial-validation}
474474

475-
When [streaming responses](#streaming-model-responses), you can pass `allow_partial=True` to `validate_response_output` to make Pydantic output validators use partial validation.
476-
477-
By adding a `partial: bool` parameter as the last argument, you can access this information within output validator functions as well.
475+
If your output validator takes `RunContext` as a first argument, you can add `partial: bool` as the last argument to handle validation differently for partial outputs during streaming (e.g. skip validation altogether).
478476

479477
```python
480478
from pydantic_ai import Agent, ModelRetry, RunContext
@@ -486,8 +484,8 @@ def validate_output(ctx: RunContext, output: str, partial: bool) -> str:
486484
if partial:
487485
return output
488486
else:
489-
if 'invalid' in output:
490-
raise ModelRetry('Output contains invalid content')
487+
if len(output) < 100:
488+
raise ModelRetry('Output is too short.')
491489
return output
492490
```
493491

pydantic_ai_slim/pydantic_ai/_output.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,13 @@
6060
| Callable[[RunContext[AgentDepsT], OutputDataT_inv], Awaitable[OutputDataT_inv]]
6161
| Callable[[OutputDataT_inv], OutputDataT_inv]
6262
| Callable[[OutputDataT_inv], Awaitable[OutputDataT_inv]]
63-
| Callable[[OutputDataT_inv, bool], OutputDataT_inv]
64-
| Callable[[OutputDataT_inv, bool], Awaitable[OutputDataT_inv]]
6563
)
6664
"""
67-
A function that always takes and returns the same type of data (which is the result type of an agent run), and:
65+
A function that always takes and returns the same type of data (which is the result type of an agent run). In addition:
6866
69-
* may or may not take [`RunContext`][pydantic_ai.tools.RunContext] as a first argument
70-
* may or may not take a `partial: bool` parameter as a last argument
71-
* may or may not be async
67+
* it can optionally take [`RunContext`][pydantic_ai.tools.RunContext] as a first argument
68+
* if it takes [`RunContext`][pydantic_ai.tools.RunContext] as a first argument, it can also optionally take `partial: bool` as a last argument
69+
* it can be async
7270
7371
Usage `OutputValidatorFunc[AgentDepsT, T]`.
7472
"""
@@ -171,15 +169,9 @@ class OutputValidator(Generic[AgentDepsT, OutputDataT_inv]):
171169
_is_async: bool = field(init=False)
172170

173171
def __post_init__(self):
174-
params = list(inspect.signature(self.function).parameters.values())
175-
if params:
176-
first_param = params[0]
177-
annotation = first_param.annotation
178-
self._takes_ctx = 'RunContext' in str(annotation)
179-
180-
expected_partial_index = 2 if self._takes_ctx else 1
181-
self._takes_partial = len(params) > expected_partial_index
182-
172+
sig = inspect.signature(self.function)
173+
self._takes_ctx = len(sig.parameters) > 1
174+
self._takes_partial = len(sig.parameters) > 2
183175
self._is_async = _utils.is_async_callable(self.function)
184176

185177
async def validate(

0 commit comments

Comments
 (0)