-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(runner): add metadata parameter to Runner.run_async() #3985
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
3fd6c93
ace7e09
72c6208
79277df
7329a33
a33506f
9c96a24
21a6010
39382ae
bc51efd
da3e685
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -400,6 +400,7 @@ async def run_async( | |||||
| new_message: Optional[types.Content] = None, | ||||||
| state_delta: Optional[dict[str, Any]] = None, | ||||||
| run_config: Optional[RunConfig] = None, | ||||||
| metadata: Optional[dict[str, Any]] = None, | ||||||
| ) -> AsyncGenerator[Event, None]: | ||||||
| """Main entry method to run the agent in this runner. | ||||||
|
|
||||||
|
|
@@ -417,6 +418,13 @@ async def run_async( | |||||
| new_message: A new message to append to the session. | ||||||
| state_delta: Optional state changes to apply to the session. | ||||||
| run_config: The run config for the agent. | ||||||
| metadata: Optional per-request metadata that will be passed to callbacks. | ||||||
| This allows passing request-specific context such as user_id, trace_id, | ||||||
| or memory context keys to before_model_callback and other callbacks. | ||||||
| Note: A shallow copy is made of this dictionary, so top-level changes | ||||||
| within callbacks won't affect the original. However, modifications to | ||||||
| nested mutable objects (e.g., nested dicts or lists) will affect the | ||||||
| original. | ||||||
|
|
||||||
| Yields: | ||||||
| The events generated by the agent. | ||||||
|
|
@@ -426,13 +434,16 @@ async def run_async( | |||||
| new_message are None. | ||||||
| """ | ||||||
| run_config = run_config or RunConfig() | ||||||
| # Create a shallow copy to isolate from caller's modifications | ||||||
| metadata = metadata.copy() if metadata else None | ||||||
|
||||||
| metadata = metadata.copy() if metadata else None | |
| metadata = metadata.copy() if metadata is not None else None |
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.
To prevent accidental modification of the original metadata dictionary by the caller of run_async, it's a good practice to work with a copy of the metadata. Since dictionaries are mutable, any changes made to metadata within the runner's logic would also affect the caller's original dictionary. Creating a shallow copy here isolates the runner's execution context from the caller. This is especially important as run_async is an async generator, and the caller might modify the metadata dictionary while iterating over the yielded events.
| metadata=metadata, | |
| metadata=metadata.copy() if metadata is not None else None, |
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.
To prevent potential subtle bugs, it's a good practice to clarify the copy behavior of the
metadatadictionary in the docstring. Since a shallow copy is performed, modifications to nested mutable objects within a callback will affect the original object passed by the caller. Please add a note about this to help users of the API understand this behavior and avoid unexpected side effects. For example, you could add:Note: A shallow copy is made of this dictionary, so changes to nested mutable objects will affect the original object.