Skip to content

[fix] Detect async __call__ factories in BaseFactory.is_async - #10297

Open
Lesereingrape wants to merge 1 commit into
agno-agi:mainfrom
Lesereingrape:fix/factory-async-callable
Open

Lesereingrape wants to merge 1 commit into
agno-agi:mainfrom
Lesereingrape:fix/factory-async-callable

Conversation

@Lesereingrape

@Lesereingrape Lesereingrape commented Sep 18, 2026 •

Copy link
Copy Markdown

Summary

BaseFactory.is_async() asks only inspect.iscoroutinefunction(self.factory), which is documented to return False for a callable object whose __call__ is a coroutine function. So an AgentFactory/TeamFactory/WorkflowFactory built from a class instance (class Builder: async def __call__(self, ctx): ...) is classified as synchronous, and both invoke paths go wrong:

  • await invoke_async(ctx) returns the coroutine object instead of the component, so resolve_async() — used by the AgentOS paths in agno/os/utils.py — fails with FactoryError: AgentFactory 'f1' returned coroutine, expected Agent, blaming the return type rather than the un-awaited call.
  • invoke(ctx) returns that same coroutine instead of raising the FactoryError its own docstring promises ("Raises if factory is async"), and the coroutine is dropped un-awaited: RuntimeWarning: coroutine 'Builder.__call__' was never awaited.

The fix mirrors the guard this repo already uses for the same CPython quirk in agno/scorer/code.py, agno/eval/suite.py and agno/hooks/decorator.py: also check the call target.

Closes #10296

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Improvement
  • Model update
  • Other:

Checklist

  • Code complies with style guidelines
  • Ran format/validation scripts (./scripts/format.sh and ./scripts/validate.sh)
  • Self-review completed
  • Documentation updated (comments, docstrings)
  • Examples and guides: Relevant cookbook examples have been included or updated (if applicable)
  • Tested in clean environment
  • Tests added/updated (if applicable)

Duplicate and AI-Generated PR Check

  • I have searched existing open pull requests and confirmed that no other PR already addresses this issue
  • If a similar PR exists, I have explained below why this PR is a better approach
  • Check if this PR was entirely AI-generated (by Copilot, Claude Code, Cursor, etc.)

Additional Notes

What changed — two lines in is_async(), no API change; invoke() and invoke_async() are untouched. Plain async def factories and functools.partial-wrapped ones were already detected correctly (iscoroutinefunction unwraps partial), so only the callable-object case changes behaviour.

How this was verified (Python 3.12 on Windows, no DB / network / API key):

  • Before: is_async() -> False; await invoke_async(ctx) -> <coroutine object>; invoke(ctx) -> <coroutine object> plus RuntimeWarning: coroutine 'AsyncBuilder.__call__' was never awaited.
  • After: is_async() -> True; await invoke_async(ctx) returns the agent with the expected id; invoke(ctx) raises FactoryError: Cannot invoke async factory synchronously. Use invoke_async() instead.
  • pytest libs/agno/tests/unit/os/test_factories.py → 25 passed (23 existing + 2 new).
  • Reverting only the source change (keeping the tests) makes exactly the 2 new tests fail, so they cover the bug rather than the mock setup.
  • ./scripts/validate.sh (agno target): ruff check libs/agno → all checks passed. mypy libs/agno --config-file libs/agno/pyproject.toml → 8 errors, all pre-existing and in unrelated files (missing optional deps in the local env: cryptography, regex stubs, dns.resolver, plus team/_run.py); none in agno/factory/.
  • ruff format --check and ruff check --select I clean on both changed files.

Collision check — searched open issues/PRs for is_async, factory, callable, then scanned the changed-file lists of all 767 open PRs: none touches any file under libs/agno/agno/factory/, and none touches tests/unit/os/test_factories.py. The nearest-sounding open PRs are unrelated in scope and files: #9208 (draft; runs sync callables off the event loop via utils/callables.py), #7621 (agent/team/tool wiring), #10180 (tools/function.py).

AI-generated disclosure: the code and tests in this PR were written by an AI coding agent, and every line of the diff was read by that same agent, which also ran every check listed above. No human has reviewed this diff, so the self-review box above is the agent's own review. (This sentence replaced the wording “reviewed line by line by the author” on 2026-09-20; a body edit leaves no public trace, so the correction is stated here.)


AI assistance (per CONTRIBUTING.md §5): the investigation, the diff and this description were produced by an AI coding agent working under the account owner's standing instruction to find and fix defects of this kind and open PRs for them; no human read the diff before it was opened. Everything quoted in the validation section was run by that agent on a local checkout and is reproducible on a fresh clone of this branch. The commit carries no AI co-author trailer.

@Lesereingrape

Copy link
Copy Markdown
Author

Real CI has not run on this PR yet - it needs one click from someone with write access.

The three workflow runs on head 94379835 (PR Lint twice and Validation) are all completed / action_required, i.e. queued and waiting for a maintainer to press Approve and run. The only check that has actually executed is the automatic Triage PR (success), which is why the merge box reads blocked: it is a missing approval, not a failing check.

The verification I do control - the ruff and mypy runs and the test results they produced - is recorded in the description above; I am not adding new claims here.

The other six PRs from this account are in the same state (#10281, #10287, #10301, #10320, #10323, #10325), each with 2-3 runs awaiting approval. The repo-wide queue is large, so I realise this is not a trivial ask; say the word and I will narrow the set down.

Disclosure: this account is being operated by an AI coding assistant on the owner's instruction, and the PR bodies say so as well.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] BaseFactory.is_async misses callable objects with an async __call__, so factories return un-awaited coroutines

1 participant