fix: streaming error for stop reason chunks (#6285)

This commit is contained in:
cthomas
2025-11-19 20:47:06 -08:00
committed by Caren Thomas
parent f6fb9aed3f
commit e4c58da682
2 changed files with 22 additions and 2 deletions

View File

@@ -1121,6 +1121,19 @@ def get_background_task_count() -> int:
@trace_method
def safe_create_task(coro, label: str = "background task"):
# Check if coro is an async generator instead of a coroutine
if inspect.isasyncgen(coro):
raise TypeError(
f"{label}: Cannot create task from async generator. "
"Async generators must be consumed with 'async for', not 'await'. "
"If you need to run an async generator as a task, wrap it in an async function."
)
if not inspect.iscoroutine(coro):
raise TypeError(
f"{label}: Expected a coroutine, got {type(coro).__name__}. Make sure you're calling the async function with () parentheses."
)
async def wrapper():
try:
await coro