Fix OpenAI agents handoff closure bug#1309
Closed
mfateev wants to merge 1 commit intotemporalio:mainfrom
Closed
Conversation
This fixes the closure late-binding bug where all handoffs would route to the last agent in the list instead of the intended target agent. The bug occurred because Python closures capture variables by reference. When iterating over handoffs and creating async wrapper functions, each closure captured `original_invoke` by reference. By the time any closure executed, `original_invoke` pointed to the last handoff's invoke function. The fix uses a factory function `_make_on_invoke_wrapper()` to properly capture the current value of `original_invoke` for each iteration. Fixes: openai/openai-agents-python#2216
Contributor
|
I think I'm going to fix it slightly differently to avoid the wrapper function, and test the user scenario fully. PR here: #1310 |
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.
Summary
_convert_agent()where all handoffs would route to the last agent in the list_make_on_invoke_wrapper()to properly capture closure variablesProblem
When creating multiple handoffs in the
_convert_agentfunction, Python's closure late-binding caused all handoffs to reference the sameoriginal_invokevariable. By the time any handoff'son_invokefunction executed,original_invokepointed to the last handoff's invoke function, causing all handoffs to route to the last agent.Solution
Use a factory function pattern to create each handoff wrapper. This ensures each closure captures the correct
original_invokevalue at the time of creation.Fixes: openai/openai-agents-python#2216
Test plan
tests/contrib/openai_agents/test_handoff_closure.pywith tests that verify correct handoff routing