Key the single-argument output wrapper by the Python parameter name so aliased params work
Closes #12336
What Problem This Solves
An output function with exactly one parameter carrying a Pydantic alias (Field(alias=...) or Field(validation_alias=...)) crashed the run with a bare KeyError:
KeyError: 'cityName'
File "pydantic_ai_slim/pydantic_ai/_output.py", line 1172, in hook_validate
return validated[k], None
FunctionSchema.single_field_name is used as the key to peel the semantic value off the validated args dict. It was derived from the single key in the JSON schema's properties — which is the alias — but validation output is always keyed by the Python parameter name:
def f(city: Annotated[str, Field(alias='cityName')]) -> str: ...
# json schema: {'properties': {'cityName': ...}} -> single_field_name == 'cityName'
# validator output: {'city': 'Paris'} -> KeyError: 'cityName'
hook_validate runs on every output validation, not just when output hooks are registered, so this hit tool output, PromptedOutput, NativeOutput and lists of output functions. Multi-arg functions (single_field_name is None) and function tools were unaffected, which is why the bug only showed up for exactly one aliased parameter.
What Changed
_function_schema.py:FunctionSchema.single_field_nameis now a stored field instead of a property computed from the JSON schema. It is filled infunction_schema(), where the generatedfields(the TypedDict schema used by the validator, always keyed by Python name) is still in scope, so the wrapper key matches what validation actually returns. The model-like single-arg case keeps usingsingle_arg_name, which was already the Python name.tests/test_capability_output_hooks.py: two regression tests inTestHookSemanticValue— one foraliasthrough tool output (also asserts the model is handed the aliased schema), one forvalidation_aliasthroughPromptedOutputwith no hooks registered, pinning that the failure was not hook-specific.
Validation
Red first, on the two new tests only:
.venv/Scripts/python.exe -m pytest tests/test_capability_output_hooks.py -p no:randomly -q -k alias
2 failed, 155 deselected
E KeyError: 'cityName'
pydantic_ai_slim\pydantic_ai\_output.py:1172: KeyError
After the fix:
... -k alias 2 passed, 155 deselected
tests/test_capability_output_hooks.py 157 passed
tests/test_capability_output_hooks.py + test_function_schema.py
+ test_capability_output_lifecycle.py
+ test_agent_output_schemas.py 234 passed
tests/test_agent.py 461 passed, 3 failed
The 3 test_agent.py failures are the pre-existing MCP subprocess tests on this machine (McpError / RuntimeError: Client failed to connect: Blocking call to os.access); they fail identically on main and are unrelated to this change.
ruff check and ruff format --check are clean on both changed files. Coverage of _function_schema.py: the new statements and both sides of the new condition are covered (remaining gaps in that module are pre-existing: signature() failures and find_typed_parameter/validate_schema_signature error paths).
pkg:core cannot be applied by an external contributor (no triage permission), so it needs a maintainer.
Summary by cubic
Fixes single-argument output functions with an aliased parameter (Field(alias=...) or Field(validation_alias=...)) crashing with KeyError. The wrapper key was derived from the JSON schema property name (the alias), but validation output is keyed by the Python parameter name, so unwrapping the semantic value failed. This affected tool output, PromptedOutput, NativeOutput, and lists of output functions. Closes #12336.
Bug Fixes
single_field_nameis now a stored field computed by a_single_field_namehelper from the generated fields (keyed by Python names) rather than the JSON schema property; this also keepsfunction_schemaunder the cognitive complexity gate.- Adds regression tests for both
Field(alias=...)andField(validation_alias=...), including a path with no output hooks registered.