Skip to content

fix(handoffs): await on_handoff callables with async __call__#3211

Merged
seratch merged 1 commit intoopenai:mainfrom
adityasingh2400:fix/handoffs-await-callable-async-call
May 8, 2026
Merged

fix(handoffs): await on_handoff callables with async __call__#3211
seratch merged 1 commit intoopenai:mainfrom
adityasingh2400:fix/handoffs-await-callable-async-call

Conversation

@adityasingh2400
Copy link
Copy Markdown
Contributor

Summary

handoff(on_handoff=...) accepts any callable, but the dispatch in
src/agents/handoffs/__init__.py decided whether to await based on
inspect.iscoroutinefunction(on_handoff). That predicate returns False for
class instances whose __call__ is async def, so for callbacks such as

class MyHook:
    async def __call__(self, ctx, input):
        ...

handoff(agent, input_type=Foo, on_handoff=MyHook())

the runtime called MyHook()(ctx, input), got a coroutine back, and discarded
it without awaiting. Result: the user's on_handoff never executed and Python
emitted RuntimeWarning: coroutine '__call__' was never awaited.

The fix mirrors the pattern already used elsewhere in the codebase (e.g.
Handoff._is_enabled, tool.py): invoke the callable, then await the
result iff it's awaitable. Sync callables remain synchronous; native async
functions and async-__call__ instances are now awaited correctly.

Test plan

  • New test test_callable_class_with_async_dunder_call_is_awaited covers
    both the input_type-bearing branch and the no-input branch.
  • pytest tests/test_handoff_tool.py tests/test_handoff_history_duplication.py tests/test_handoff_prompt.py (28 passed).
  • ruff check + ruff format --check clean on the touched files.
  • Confirmed the new test fails on main with the documented RuntimeWarning before applying the fix.

@github-actions github-actions Bot added bug Something isn't working feature:core labels May 8, 2026
@seratch
Copy link
Copy Markdown
Member

seratch commented May 8, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@seratch seratch merged commit 89f368d into openai:main May 8, 2026
10 checks passed
@seratch seratch added this to the 0.17.x milestone May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working feature:core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants