Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

📥 Pull Request

📘 Description

Fixes #1262 - Resolves TypeError when using async for with Agno Team.arun() streaming.

Root Cause: When Team.arun(stream=True) is called, it returns an async generator directly (not a coroutine). The team async wrapper was defined as async def, which wrapped the returned async generator in a coroutine, causing the error: 'async for' requires an object with __aiter__ method, got coroutine.

Solution:

  1. Added AsyncStreamingResultWrapper class that properly implements __aiter__ and __anext__ for async generator wrapping
  2. Changed create_team_async_wrapper from async def to regular def to prevent wrapping async generators in coroutines
  3. Added inspect.isasyncgen() checks to detect and handle async generators correctly
  4. Updated create_streaming_agent_async_wrapper to check for __aiter__ before __iter__

Changes (Core Fix):

  • agentops/instrumentation/agentic/agno/instrumentor.py: Added AsyncStreamingResultWrapper class and modified team/agent async wrappers to properly handle async generators

🧪 Testing

Created test script test_async_streaming_team.py that reproduces the issue:

  • ✅ Successfully started async iteration (no more coroutine error)
  • ✅ Received RunStartedEvent showing async streaming is working
  • ⚠️ Hit OpenAI quota limit during test (unrelated to fix)
  • ✅ Linting passed: ruff check and ruff format both pass

Evidence of Fix:

  • No more RuntimeWarning: coroutine 'create_team_async_wrapper.<locals>.wrapper' was never awaited
  • No more TypeError: 'async for' requires an object with __aiter__ method
  • Async iteration successfully started with proper event streaming

⚠️ CRITICAL: Unrelated Changes Detected

This PR contains many unrelated files that should NOT be included:

  • .envrc
  • Multiple setup guides (SETUP_GUIDE.md, local_supabase_linux.md, local_clickhouse_setup.md)
  • Memori integration documentation
  • Environment file updates
  • README updates

Action Required: These files should be excluded from this PR and submitted separately if needed.


🔍 Human Review Checklist

High Priority:

  1. ⚠️ Verify ONLY the instrumentor.py changes should be merged - exclude all unrelated files
  2. Confirm AsyncStreamingResultWrapper.__anext__ correctly maintains span context
  3. Verify changing create_team_async_wrapper from async def to def is the correct solution
  4. Check that inspect.isasyncgen() is the right check for detecting async generators

Medium Priority:
5. Verify the fix works for both Team.arun() and Agent.arun() streaming
6. Confirm span cleanup happens correctly in StopAsyncIteration case
7. Check if similar issues exist in other async wrappers (workflow, team_internal)


Session Info:

areibman and others added 3 commits September 2, 2025 23:44
Fixes #1262

The issue was that Team.arun() with stream=True returns an async generator
directly, not a coroutine. The team async wrapper was defined as 'async def'
which caused it to wrap the async generator in a coroutine, leading to the
error: 'async for' requires an object with __aiter__ method, got coroutine.

Changes:
- Added AsyncStreamingResultWrapper class to properly wrap async generators
- Modified create_team_async_wrapper to be a regular function (not async)
- Added inspect.isasyncgen() check to detect async generators
- Updated create_streaming_agent_async_wrapper to check for __aiter__ first

The fix ensures that async generators are returned directly without being
wrapped in a coroutine, allowing proper async iteration with 'async for'.

Co-Authored-By: Alex <meta.alex.r@gmail.com>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@devin-ai-integration
Copy link
Contributor Author

Closing this PR due to unrelated files being included. Opening a clean PR with only the fix.

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]: AgentOps breaks async streaming in Agno framework

2 participants