Widen RunHooks.on_tool_end result type to Any#3517
Closed
NishchayMahor wants to merge 1 commit into
Closed
Conversation
Tools can return any JSON-serialisable value — strings, dicts, Pydantic models, lists. The hook signature claimed str, but the runtime forwards whatever the tool returned (after output guardrails), so any function tool emitting structured output trips the contract once strict typing gets involved. Change RunHooksBase.on_tool_end and AgentHooksBase.on_tool_end to take result: Any and document what callers actually receive. No call sites change shape — they were already passing through the raw value. Closes openai#3512
Member
|
Thanks for sharing this. Resolved by #3518 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #3512.
Summary
Tools can return any JSON-serialisable value — strings, Pydantic models, dicts, lists. The runtime forwards whatever the tool returned (after output guardrails) straight into `hooks.on_tool_end`:
```python
src/agents/run_internal/tool_execution.py
final_result = await _execute_tool_output_guardrails(...) # -> Any
self.hooks.on_tool_end(tool_context, self.public_agent, func_tool, final_result)
```
But `RunHooksBase.on_tool_end` and `AgentHooksBase.on_tool_end` declared `result: str`. With strict type checking that triggers:
```
TypeError: Argument 'result' has incorrect type (expected str, got dict)
```
The fix is to align the hook signature with what the runtime actually passes — widen `result` to `Any` and document what the parameter contains.
What changed
No call sites changed shape — they were already passing through the raw value.
Compatibility
Backward-compatible widening. Existing overrides that typed the parameter `str` keep working because `str` is a subtype of `Any`; the only behavioural change is that strict runtime type checks now accept the dict/list/Pydantic-model values the runtime was already passing in.
Verification
```
$ uv run pytest tests/test_run_hooks.py tests/test_global_hooks.py tests/test_agent_llm_hooks.py -q
21 passed in 0.18s
$ uv run ruff check src/agents/lifecycle.py tests/test_run_hooks.py
All checks passed!
$ uv run pyright src/agents/lifecycle.py tests/test_run_hooks.py
0 errors, 0 warnings, 0 informations
```