Skip to content

fix(tracing): preserve empty-string metadata values#834

Open
i-anubhav-anand wants to merge 1 commit into
langfuse:mainfrom
i-anubhav-anand:fix/empty-string-metadata
Open

fix(tracing): preserve empty-string metadata values#834
i-anubhav-anand wants to merge 1 commit into
langfuse:mainfrom
i-anubhav-anand:fix/empty-string-metadata

Conversation

@i-anubhav-anand

@i-anubhav-anand i-anubhav-anand commented Jun 14, 2026

Copy link
Copy Markdown

Empty-string metadata values are silently dropped. In _flattenAndSerializeMetadata:

const serialized = typeof value === "string" ? value : _serialize(value);
if (serialized) {                       // "" is falsy -> dropped
  metadataAttributes[`${prefix}.${key}`] = serialized;
}

So metadata: { a: "", b: "x", c: 0, d: false } emits b, c ("0"), d ("false") but silently drops a — inconsistent, since 0 and false survive.

_serialize returns undefined only for null/undefined, so switching the guard to if (serialized !== undefined) preserves "" (and any other falsy-but-present value) while still skipping null/undefined. Applied to both the scalar-metadata branch and the object-entry loop.

Test

Adds an integration test (tracing.integration.test.ts) asserting an empty-string metadata value is exported. Fails before (attribute absent), passes after. prettier/eslint/tsc clean (pre-commit hook).

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Greptile Summary

This PR fixes a silent data-loss bug in _flattenAndSerializeMetadata where empty-string metadata values ("") were dropped because the truthiness guard if (serialized) evaluated "" as falsy. The fix changes both guards to serialized !== undefined, which aligns with _serialize's contract of returning undefined only for null/undefined inputs.

  • packages/tracing/src/attributes.ts: Two if (serialized) guards replaced with if (serialized !== undefined) in the scalar and object-entry branches of _flattenAndSerializeMetadata.
  • tests/integration/tracing.integration.test.ts: Integration test added that verifies an empty-string metadata value appears as a span attribute after export.

Confidence Score: 5/5

Safe to merge — the change is a minimal, targeted guard fix that preserves existing behavior for all non-empty values while correctly retaining empty strings.

The two-line change is narrowly scoped: _serialize is guaranteed to return either a string or undefined, so !== undefined is the correct predicate. All previously-passing values (non-empty strings, numbers, booleans, objects) continue to pass the new guard unchanged, and only the previously-dropped "" case is now correctly preserved. The integration test directly validates the fixed scenario.

No files require special attention — both changed files are straightforward and self-contained.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["_flattenAndSerializeMetadata(metadata, type)"] --> B{metadata null/undefined?}
    B -- Yes --> C[return empty object]
    B -- No --> D{Is metadata non-object or Array?}
    D -- Yes --> E["serialized = _serialize(metadata)"]
    E --> F{"serialized !== undefined?\n(was: if serialized)"}
    F -- Yes --> G["metadataAttributes[prefix] = serialized"]
    F -- No --> C
    D -- No --> H["for each [key, value] in metadata"]
    H --> I{"typeof value === 'string'?"}
    I -- Yes --> J["serialized = value (direct, e.g. '')"]    
    I -- No --> K["serialized = _serialize(value)"]
    J --> L{"serialized !== undefined?\n(was: if serialized — dropped '')"}
    K --> L
    L -- Yes --> M["metadataAttributes[prefix.key] = serialized"]
    L -- No --> H
    G --> N[return metadataAttributes]
    M --> N
Loading

Reviews (1): Last reviewed commit: "fix(tracing): preserve empty-string meta..." | Re-trigger Greptile

_flattenAndSerializeMetadata gated each serialized metadata value behind a
truthiness check (`if (serialized)`), which silently dropped empty-string
values while keeping 0 and false. So metadata { a: "", b: "x" } emitted only
b. _serialize only returns undefined for null/undefined, so checking
`!== undefined` keeps "" (and other falsy-but-present values) while still
skipping null/undefined. Applied to both the scalar and object-entry paths.

Adds an integration test asserting an empty-string metadata value is exported
(fails before: the attribute was absent).

@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.

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.

@vercel

vercel Bot commented Jun 14, 2026

Copy link
Copy Markdown

@i-anubhav-anand is attempting to deploy a commit to the langfuse Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Jun 14, 2026

Copy link
Copy Markdown

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