fix(core): async tracer on_chat_model_start fallback in sync context#7
fix(core): async tracer on_chat_model_start fallback in sync context#7DhirenMhatre wants to merge 1 commit into
on_chat_model_start fallback in sync context#7Conversation
|
CodeAnt AI is reviewing your PR. |
There was a problem hiding this comment.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
PR SummaryWhat Changed
Key Changes by AreaTracing/Callbacks: Added Testing: Added three test cases in Files Changed
Review Focus Areas
ArchitectureDesign Decisions: The fallback is intentionally silent (no logs) to avoid noise during normal operation. This trades observability for user experience, assuming missing Risks: The broad Merge StatusMERGEABLE — PR Score 75/100, above threshold (50). All gates passed. |
Workflow DiagramsAutomatically generated sequence diagrams showing the workflows in this PR 1. Async Callback Handler Fallback Flow_Medium complexity • Components: handle_event, achat_model_start_fallback, AsyncBaseTracer sequenceDiagram
title Async Chat Model Start Fallback in Sync Context
participant Caller as Sync Caller
participant HE as handle_event
participant ACH as AsyncCallbackHandler
participant ABF as _achat_model_start_fallback
participant AEH as _ahandle_event_for_handler
participant RC as _run_coros
Note over Caller,RC: PR: Fix async tracer fallback when on_chat_model_start not implemented
Caller->>HE: handle_event(handlers, "on_chat_model_start", ...)
loop For each handler
HE->>ACH: getattr(handler, "on_chat_model_start")(*args, **kwargs)
alt Handler returns coroutine (async)
HE->>HE: asyncio.iscoroutine(event) == True
alt event_name is "on_chat_model_start"
HE->>ABF: _achat_model_start_fallback(coro, handler, *args, **kwargs)
ABF->>ACH: await coro (original on_chat_model_start)
alt NotImplementedError raised
ABF->>ABF: Catch NotImplementedError
ABF->>ABF: Convert messages to prompts via get_buffer_string
ABF->>AEH: _ahandle_event_for_handler(handler, "on_llm_start", ...)
AEH->>ACH: handler.on_llm_start(serialized, prompts, ...)
ACH-->>AEH: Return/await
AEH-->>ABF: Complete
ABF-->>HE: Complete (fallback successful)
else No error
ACH-->>ABF: Return normally
ABF-->>HE: Complete (no fallback needed)
end
HE->>HE: coros.append(wrapped_coro)
else Other event
HE->>HE: coros.append(event)
end
else Handler raises NotImplementedError (sync path)
HE->>HE: Catch NotImplementedError
alt event_name is "on_chat_model_start"
HE->>HE: Convert messages to prompts
HE->>HE: Call handler.on_llm_start(...) directly
end
end
end
HE->>RC: _run_coros(coros) (execute all collected coroutines)
RC-->>HE: All coroutines complete
HE-->>Caller: Return
Note: Diagrams show detected patterns only. Complex workflows may require manual review. |
|
CodeAnt AI finished reviewing your PR. |
| SERIALIZED = {"id": ["chat_model"]} | ||
|
|
||
|
|
||
| class _NoOpAsyncTracer(AsyncBaseTracer): |
There was a problem hiding this comment.
The _NoOpAsyncTracer class inherits from AsyncBaseTracer, which provides a fully working on_chat_model_start implementation. This prevents the intended fallback mechanism from being tested, as the fallback to on_llm_start only triggers when on_chat_model_start raises NotImplementedError. To properly exercise the fallback code path, _NoOpAsyncTracer should inherit from AsyncCallbackHandler instead, which raises NotImplementedError for on_chat_model_start.
Also reported at: libs/core/tests/unit_tests/callbacks/test_handle_event.py L136–L155
| class _NoOpAsyncTracer(AsyncBaseTracer): | |
| class _NoOpAsyncTracer(AsyncCallbackHandler): |
Prompt for AI assistance
Copy the prompt below and paste it into ChatGPT, Claude, or any LLM:
You are an expert bash developer with deep knowledge of security, performance, and best practices.
### Context
File: libs/core/tests/unit_tests/callbacks/test_handle_event.py
Lines: 20-20
Issue Type: functional-medium
Severity: medium
Issue Description:
The `_NoOpAsyncTracer` class inherits from `AsyncBaseTracer`, which provides a fully working `on_chat_model_start` implementation. This prevents the intended fallback mechanism from being tested, as the fallback to `on_llm_start` only triggers when `on_chat_model_start` raises `NotImplementedError`. To properly exercise the fallback code path, `_NoOpAsyncTracer` should inherit from `AsyncCallbackHandler` instead, which raises `NotImplementedError` for `on_chat_model_start`.
_Also reported at: `libs/core/tests/unit_tests/callbacks/test_handle_event.py` L136–L155_
Current Code:
class _NoOpAsyncTracer(AsyncBaseTracer):
---
### Instructions
1. Fix the issue described above
2. Maintain the exact indentation and code style from the original
3. Follow bash best practices and language-specific idioms
4. Ensure the fix addresses the root cause, not just the symptoms
5. Add brief inline comments explaining the fix if needed
### Constraints
- Do not change functionality beyond fixing the identified issue
- Preserve existing variable names and function signatures unless they are part of the problem
- Ensure the fix is production-ready
---
Security Scan Summary
No critical security issues detected Scan completed in 21.7sSecurity scan powered by Codity.ai |
License Compliance Scan
All licenses are low-risk and compliant Powered by Codity.ai · Docs |
Code Quality Report — test-org-codity/langchain · PR #7Scanned: 2026-06-06 19:01 UTC | Score: 61/100 | Provider: github Executive Summary
Top Findings[CQ-LLM-004]
|
| File | Critical | High | Medium | Low | Total |
|---|---|---|---|---|---|
libs/core/langchain_core/callbacks/manager.py |
0 | 0 | 2 | 5 | 7 |
libs/core/tests/unit_tests/callbacks/test_handle_event.py |
0 | 1 | 0 | 3 | 4 |
Recommendations
- Resolve High severity issues, especially error handling gaps and performance bottlenecks.
- Run automated tests after applying fixes to verify no regressions.
User description
Fixes #
Read the full contributing guidelines: https://docs.langchain.com/oss/python/contributing/overview
If you paste a large clearly AI generated description here your PR may be IGNORED or CLOSED!
Thank you for contributing to LangChain! Follow these steps to have your pull request considered as ready for review.
Fixes #xxline at the top is required for external contributions — update the issue number and keep the keyword. This links your PR to the approved issue and auto-closes it on merge.make format,make lintandmake testfrom the root of the package(s) you've modified.Additional guidelines:
uv.lockfiles or add dependencies topyproject.tomlfiles (even optional ones) unless you have explicit permission to do so by a maintainer.Social handles (optional)
Twitter: @
LinkedIn: https://linkedin.com/in/
CodeAnt-AI Description
Async chat model callbacks now fall back to LLM start handling in sync code
What Changed
Impact
✅ Fewer missing chat traces✅ More reliable async tracing in sync code✅ Clearer callback behavior for unsupported chat events💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
Preserve Org Learnings with CodeAnt
You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.