Skip to content

fix(langchain): handle null tokenUsage - #769

Merged
hassiebp merged 1 commit into
mainfrom
fix-null-tokenUsage-langchain-vertex-v4
Apr 1, 2026
Merged

fix(langchain): handle null tokenUsage#769
hassiebp merged 1 commit into
mainfrom
fix-null-tokenUsage-langchain-vertex-v4

Conversation

@hassiebp

@hassiebp hassiebp commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator

Disclaimer: Experimental PR review

Greptile Summary

This PR is a targeted one-line fix for a crash in handleLLMEnd within the LangChain callback handler. When extractUsageMetadata returns undefined and output.llmOutput?.["tokenUsage"] is explicitly null (rather than undefined), the nullish-coalescing chain previously resolved to null. This caused a TypeError on the following lines that use the in operator (e.g., "promptTokens" in llmUsage), since in cannot operate on null. Adding ?? {} as a final fallback ensures llmUsage is always a safe object.

  • Root cause: ?? only guards against null/undefined on its left side; it does not prevent null from propagating as the resolved value when that null is itself the result of the chain.
  • Fix: Appending ?? {} guarantees llmUsage is at minimum an empty object, making all downstream in checks and optional-chain accesses safe.
  • Impact: No functional regression — an empty object produces all-undefined usage fields, which is the same effective result as before (no usage data), but without the crash.

Confidence Score: 5/5

Safe to merge — the fix is minimal, correct, and eliminates a crash without changing intended behavior.

No P0 or P1 issues found. The one-line change correctly addresses the null-crash bug, all downstream consumers of llmUsage already handle undefined values gracefully via optional chaining, and the empty-object fallback is a well-established pattern for this type of nullish guard.

No files require special attention.

Important Files Changed

Filename Overview
packages/langchain/src/CallbackHandler.ts Adds a ?? {} fallback so llmUsage is never null when output.llmOutput?.["tokenUsage"] is explicitly null, preventing a TypeError from the subsequent "in" operator checks.

Sequence Diagram

sequenceDiagram
    participant LC as LangChain
    participant CB as CallbackHandler
    participant EU as extractUsageMetadata()

    LC->>CB: handleLLMEnd(output, runId)
    CB->>CB: get lastResponse from output.generations
    CB->>EU: extractUsageMetadata(lastResponse)
    EU-->>CB: UsageMetadata | undefined

    alt extractUsageMetadata returned value
        CB->>CB: llmUsage = UsageMetadata
    else null/undefined → check tokenUsage
        CB->>CB: llmUsage = output.llmOutput?.["tokenUsage"]
        alt tokenUsage is null (bug case fixed by PR)
            CB->>CB: llmUsage = {} (fallback default)
        end
    end

    CB->>CB: build usageDetails { input, output, total }
    Note over CB: "in" operator safe — llmUsage is never null
    CB->>CB: handleOtelSpanEnd(runId, usageDetails, ...)
Loading

Reviews (1): Last reviewed commit: "fix(langchain): handle null tokenUsage" | Re-trigger Greptile

@vercel

vercel Bot commented Apr 1, 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 Apr 1, 2026 8:39am

Request Review

@github-actions

github-actions Bot commented Apr 1, 2026

Copy link
Copy Markdown

@claude review

@hassiebp
hassiebp enabled auto-merge (squash) April 1, 2026 08:38
@hassiebp
hassiebp disabled auto-merge April 1, 2026 08:38
@hassiebp
hassiebp merged commit 3d7c98b into main Apr 1, 2026
7 of 8 checks passed
@hassiebp
hassiebp deleted the fix-null-tokenUsage-langchain-vertex-v4 branch April 1, 2026 08:38

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM — simple null-safety fix.

Extended reasoning...

Overview

Single-line change in packages/langchain/src/CallbackHandler.ts: adds ?? {} as a final fallback when computing llmUsage in handleLLMEnd. This prevents llmUsage from being null when output.llmOutput["tokenUsage"] is explicitly null (not undefined), which the ?? operator would not catch without this addition.

Security risks

None. This is purely a defensive null-guard on an internal value; no auth, crypto, or data-exposure paths are touched.

Level of scrutiny

Low. The change is mechanical and isolated to a single expression. The downstream usage of llmUsage includes "promptTokens" in llmUsage, which would throw a TypeError on a null value — the empty-object fallback is the correct fix.

Other factors

No bugs reported by the automated hunting system. Change touches no CODEOWNER-sensitive paths and follows the existing pattern in the file.

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.

1 participant