Skip to content

Fix swallowed TypeError in async generator wrapper fallback#1659

Open
Parth-001-git wants to merge 1 commit into
langfuse:mainfrom
Parth-001-git:fix-async-generator-typeerror
Open

Fix swallowed TypeError in async generator wrapper fallback#1659
Parth-001-git wants to merge 1 commit into
langfuse:mainfrom
Parth-001-git:fix-async-generator-typeerror

Conversation

@Parth-001-git
Copy link
Copy Markdown

@Parth-001-git Parth-001-git commented May 19, 2026

Summary

Replaces broad except TypeError fallback logic with explicit sys.version_info checks when handling async generator task creation.

This prevents unrelated user TypeError exceptions from being silently swallowed inside _ContextPreservedAsyncGeneratorWrapper.__anext__.

Reproduction

The issue can be reproduced using the async generator example from #12520, where a user-side TypeError was previously swallowed and iteration completed silently.

Fix

  • Replaced runtime except TypeError fallback with explicit Python version branching.
  • Preserves Python 3.10 compatibility behavior.
  • Allows genuine user exceptions to propagate correctly.

Greptile Summary

This PR fixes a long-standing bug where a broad except TypeError catch in _ContextPreservedAsyncGeneratorWrapper.__anext__ and aclose silently swallowed any TypeError raised by the user's generator, replacing it with an explicit sys.version_info >= (3, 11) branch that reflects the true Python version boundary at which asyncio.create_task gained its context keyword argument.

  • Core fix: User-thrown TypeErrors inside async generators now propagate correctly instead of being silently caught and re-routed through the Python 3.10 fallback path.
  • Scope: Both aclose and __anext__ receive the same fix; the sys module is added at the top of the module (compliant with the repo import rule).
  • Style nit: The version check predicate is duplicated in two methods and import sys is inserted ahead of import asyncio, breaking the existing alphabetical import order.

Confidence Score: 4/5

Safe to merge — the changed code correctly replaces runtime exception catching with an explicit Python version check, and behavior on both code paths is preserved.

The logic change is accurate: asyncio.create_task's context keyword was introduced in Python 3.11, so the version guard is the right boundary. Error propagation through the outer except block is unchanged. The only remaining notes are style-level: the duplicated version predicate and the out-of-order import sys line.

No files require special attention; both affected methods in langfuse/_client/observe.py received the same treatment consistently.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["__anext__ called"] --> B{sys.version_info >= 3.11?}
    B -- Yes --> C["asyncio.create_task(generator.__anext__(), context=self.context)"]
    B -- No --> D["self.context.run(asyncio.create_task, generator.__anext__())"]
    C --> E{Exception?}
    D --> E
    E -- StopAsyncIteration --> F["_finalize() + re-raise"]
    E -- Exception / CancelledError --> G["_finalize_with_error(e) + re-raise"]
    E -- No exception --> H["Append item if capture_output"]
    H --> I["return item"]

    J["aclose called"] --> K{sys.version_info >= 3.11?}
    K -- Yes --> L["asyncio.create_task(generator.aclose(), context=self.context)"]
    K -- No --> M["self.context.run(asyncio.create_task, generator.aclose())"]
    L --> N{Exception?}
    M --> N
    N -- Yes --> O["_finalize_with_error(error) + re-raise"]
    N -- No --> P["_finalize()"]
Loading
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
langfuse/_client/observe.py:1-2
The `sys.version_info >= (3, 11)` predicate is evaluated on every `aclose` and `__anext__` call and duplicated in two places. A single module-level constant avoids both the minor repetition and makes the intent obvious to future readers.

```suggestion
import sys
import asyncio

# asyncio.create_task(context=...) was added in Python 3.11
_TASK_CONTEXT_KWARG_SUPPORTED = sys.version_info >= (3, 11)
```

### Issue 2 of 2
langfuse/_client/observe.py:1-5
Standard library imports are ordered alphabetically throughout the existing block (`asyncio`, `contextvars`, `inspect`, `os`). Inserting `sys` at position 1 breaks that ordering; it should follow `os`.

```suggestion
import asyncio
import contextvars
import inspect
import os
import sys
```

Reviews (1): Last reviewed commit: "Fix swallowed TypeError in async generat..." | Re-trigger Greptile

Context used:

  • Rule used - Move imports to the top of the module instead of p... (source)

Learned From
langfuse/langfuse-python#1387

Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 19, 2026

CLA assistant check
All committers have signed the CLA.

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.

2 participants