fix(langchain): do not throw in callback handler if usageDetails are unavailable - #767
Closed
marliessophie wants to merge 1 commit into
Closed
fix(langchain): do not throw in callback handler if usageDetails are unavailable#767marliessophie wants to merge 1 commit into
marliessophie wants to merge 1 commit into
Conversation
|
@claude review |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Collaborator
|
Closed in favor of #769 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Disclaimer: Experimental PR review
Greptile Summary
This PR fixes a
TypeErrorinhandleLLMEndwhere theinoperator was applied tollmUsagewithout first verifying it was a non-null object. WhenllmUsageisundefined— e.g. when neitherextractUsageMetadatanoroutput.llmOutput?.["tokenUsage"]returns a value — expressions like"promptTokens" in llmUsagewould throw instead of gracefully producingundefined.Key changes:
usageObjguard that castsllmUsagetoRecord<string, any>only when it is both truthy and an object, safely handlingundefined,null, and primitive values.usageObj, which is semantically equivalent for valid objects and safe for non-objects.llmUsageis a well-formedUsageMetadataor OpenAItokenUsageobject; only the crash path is eliminated.Confidence Score: 5/5
Safe to merge — the fix correctly eliminates a real TypeError crash with no behaviour change on the happy path.
The single changed file addresses a well-scoped bug (TypeError from
inonundefined). No new logic is introduced, existing behaviour on valid objects is preserved, and all remaining feedback is a P2 style suggestion (redundant?? undefinedtrailing expressions) that does not affect correctness.No files require special attention.
Important Files Changed
llmUsageagainst non-object values before property-existence checks, preventing aTypeErrorthrown by theinoperator whenllmUsageisundefined; logic and behavior preserved in all non-null paths.Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[handleLLMEnd called] --> B[extractUsageMetadata] B --> C{UsageMetadata returned?} C -- yes --> D[llmUsage = UsageMetadata] C -- no --> E[llmUsage = output.llmOutput?.tokenUsage] E --> F{llmUsage defined and is object?} D --> F F -- yes --> G[usageObj = llmUsage as Record] F -- no --> H[usageObj = undefined] G --> I[Build usageDetails via optional chaining on usageObj] H --> I I --> J[Augment with input_token_details and output_token_details if present] J --> K[handleOtelSpanEnd with usageDetails]Reviews (1): Last reviewed commit: "fix(langchain): do not throw in callback..." | Re-trigger Greptile