Skip to content

fix(langchain): do not throw in callback handler if usageDetails are unavailable - #767

Closed
marliessophie wants to merge 1 commit into
mainfrom
marlies/lfe-fix-vertex-completion-issues
Closed

fix(langchain): do not throw in callback handler if usageDetails are unavailable#767
marliessophie wants to merge 1 commit into
mainfrom
marlies/lfe-fix-vertex-completion-issues

Conversation

@marliessophie

@marliessophie marliessophie commented Mar 31, 2026

Copy link
Copy Markdown
Member

Disclaimer: Experimental PR review

Greptile Summary

This PR fixes a TypeError in handleLLMEnd where the in operator was applied to llmUsage without first verifying it was a non-null object. When llmUsage is undefined — e.g. when neither extractUsageMetadata nor output.llmOutput?.["tokenUsage"] returns a value — expressions like "promptTokens" in llmUsage would throw instead of gracefully producing undefined.

Key changes:

  • Introduces a usageObj guard that casts llmUsage to Record<string, any> only when it is both truthy and an object, safely handling undefined, null, and primitive values.
  • Replaces explicit property-existence checks with optional chaining on usageObj, which is semantically equivalent for valid objects and safe for non-objects.
  • Behavior is fully preserved when llmUsage is a well-formed UsageMetadata or OpenAI tokenUsage object; 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 in on undefined). No new logic is introduced, existing behaviour on valid objects is preserved, and all remaining feedback is a P2 style suggestion (redundant ?? undefined trailing expressions) that does not affect correctness.

No files require special attention.

Important Files Changed

Filename Overview
packages/langchain/src/CallbackHandler.ts Guards llmUsage against non-object values before property-existence checks, preventing a TypeError thrown by the in operator when llmUsage is undefined; 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]
Loading

Reviews (1): Last reviewed commit: "fix(langchain): do not throw in callback..." | Re-trigger Greptile

@github-actions

Copy link
Copy Markdown

@claude review

@vercel

vercel Bot commented Mar 31, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
langfuse-js Ready Ready Preview Mar 31, 2026 1:51pm

Request Review

@hassiebp

hassiebp commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator

Closed in favor of #769

@wochinge
wochinge deleted the marlies/lfe-fix-vertex-completion-issues branch June 11, 2026 13:09
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