-
Notifications
You must be signed in to change notification settings - Fork 893
Python: Added custom args and thread object to ai_function kwargs #2769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Python Test Coverage Report •
Python Unit Test Overview
|
|||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR enables passing custom keyword arguments and thread objects from agent.run() to AI functions via **kwargs, allowing tools to access runtime context without exposing these parameters to the AI model.
Key changes:
- AI functions can now receive runtime kwargs like
user_idorthreadobjects - The
**kwargsparameters are automatically excluded from function schemas - Thread objects are automatically injected into tool invocations
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
python/packages/core/agent_framework/_tools.py |
Added detection logic for **kwargs in function signatures and excluded VAR_KEYWORD parameters from schema generation. Modified AIFunction.__init__ to set _forward_runtime_kwargs flag and updated _create_input_model_from_func to filter out variadic parameters. |
python/packages/core/agent_framework/_agents.py |
Modified run and run_stream methods to inject thread into kwargs before passing to chat client, and filter thread from kwargs when calling _notify_thread_of_new_messages to avoid duplicate arguments. |
python/packages/core/tests/core/test_tools.py |
Added comprehensive test test_ai_function_with_kwargs_injection to verify kwargs injection works correctly, schema excludes kwargs, and both direct invocation and invoke method handle kwargs properly. |
python/packages/core/tests/core/test_agents.py |
Added test test_agent_tool_receives_thread_in_kwargs to verify that thread objects are properly passed to tools through kwargs during agent execution with message stores. |
python/samples/getting_started/tools/ai_function_with_kwargs.py |
New sample demonstrating how to inject custom arguments like user_id into AI functions for passing runtime context without exposing it to the AI model. |
python/samples/getting_started/tools/ai_function_with_thread_injection.py |
New sample showing how to access the thread object inside AI functions via kwargs, enabling tools to query conversation history and thread metadata. |
python/samples/getting_started/tools/README.md |
Updated documentation to include references to the two new sample files demonstrating kwargs injection and thread injection patterns. |
|
@dmytrostruk looks like we'll need to pop the thread before calling Should we also make sure we have unit test coverage checking for the proper args? We shouldn't need to wait until failing integration tests to see these types of errors. |
Motivation and Context
Resolves: #2622
Updated agent and tool invocation logic to include kwargs and thread object from
agent.runmethod inai_function.Contribution Checklist