Skip to content

fix: coerce non-string metadata values instead of dropping them#1575

Open
ChunkyTortoise wants to merge 1 commit intolangfuse:mainfrom
ChunkyTortoise:fix/coerce-non-string-metadata
Open

fix: coerce non-string metadata values instead of dropping them#1575
ChunkyTortoise wants to merge 1 commit intolangfuse:mainfrom
ChunkyTortoise:fix/coerce-non-string-metadata

Conversation

@ChunkyTortoise
Copy link

@ChunkyTortoise ChunkyTortoise commented Mar 25, 2026

Summary

  • Coerce non-string metadata values via str() in _propagate_attributes instead of dropping them with a warning
  • Matches the documented behavior in the v3→v4 migration guide: "Non-string values are automatically coerced to strings"
  • Fixes noisy warnings when using LangGraph, which injects langgraph_step (int), langgraph_triggers (list), langgraph_path (tuple) into metadata on every graph step

Changes

  • langfuse/_client/propagation.py: Convert non-string metadata values to strings before validation (3-line change)
  • tests/test_propagate_attributes.py: Add test for non-string metadata coercion with LangGraph-style metadata types

Test plan

  • New test test_non_string_metadata_values_coerced verifies int/list/tuple metadata is coerced to strings
  • Existing tests should continue to pass (string values unchanged, over-200-char values still dropped)

Fixes #1571

Disclaimer: Experimental PR review

Greptile Summary

This PR fixes a noisy-warning regression introduced in the v3→v4 migration by coercing non-string metadata values to strings instead of dropping them. The change is minimal and well-targeted: a single str(value) if not isinstance(value, str) else value guard in _propagate_attributes ensures LangGraph-injected metadata (langgraph_step: int, langgraph_triggers: list, langgraph_path: tuple) flows through rather than being silently discarded.

Key changes:

  • langfuse/_client/propagation.py: Coerce non-string metadata values before calling _validate_string_value, matching the v3→v4 migration guide's "Non-string values are automatically coerced to strings" promise.
  • tests/test_propagate_attributes.py: New test test_non_string_metadata_values_coerced validates all three LangGraph metadata types (int, list, tuple) as well as string pass-through.

Minor follow-ups worth addressing:

  • The public propagate_attributes signature still declares metadata: Optional[Dict[str, str]]; updating it to Optional[Dict[str, Any]] would prevent spurious type-checker errors for callers passing LangGraph metadata.
  • The docstring Note still says metadata values "must be strings", which contradicts the new behavior.

Confidence Score: 5/5

  • Safe to merge; the fix is a targeted 3-line change with a clear motivation, correct logic, and adequate test coverage.
  • The core logic change is minimal and correct — coercing before validation rather than dropping. The two remaining items (type annotation and docstring) are non-blocking style/doc cleanups that don't affect runtime behavior. No existing tests are broken and the new test covers the intended cases.
  • No files require special attention beyond the minor type annotation and docstring updates noted above.

Important Files Changed

Filename Overview
langfuse/_client/propagation.py Core fix coerces non-string metadata values via str() before validation. Logic is correct and minimal. Two minor follow-ups: the public function's type annotation (Dict[str, str]) and the docstring Note no longer match the new behavior.
tests/test_propagate_attributes.py New test covers int, list, and tuple coercion with realistic LangGraph metadata keys, and verifies string values pass through unchanged. Adequate coverage for the change.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["propagate_attributes(metadata=...)"] --> B{"metadata is not None?"}
    B -- No --> G["Skip metadata block"]
    B -- Yes --> C["Iterate key, value in metadata.items()"]
    C --> D{"isinstance(value, str)"}
    D -- Yes --> E["str_value = value"]
    D -- No --> F["str_value = str(value) — NEW: coerce"]
    E --> H{"len(str_value) <= 200"}
    F --> H
    H -- Pass --> I["validated_metadata[key] = str_value"]
    H -- Fail --> J["warn + drop value"]
    I --> K["_set_propagated_attribute with validated_metadata"]
Loading

Reviews (1): Last reviewed commit: "fix: coerce non-string metadata values i..." | Re-trigger Greptile

Per the v3→v4 migration guide, propagated metadata non-string values
should be "automatically coerced to strings." The implementation in
_propagate_attributes dropped them with a warning instead.

This caused noisy warnings with LangGraph, which injects non-string
metadata (langgraph_step: int, langgraph_triggers: list,
langgraph_path: tuple) into RunnableConfig on every graph step.

Fix: coerce values via str() before validation in the metadata
processing loop, matching the documented behavior.

Fixes langfuse#1571
Copy link

@claude claude bot left a comment

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.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2ac10d8244

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

validated_metadata[key] = value
# Coerce non-string values to strings per v3→v4 migration guide
# (LangGraph injects int/list/tuple metadata like langgraph_step)
str_value = str(value) if not isinstance(value, str) else value

Choose a reason for hiding this comment

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

P2 Badge Handle str failures when coercing metadata

Calling str(value) unconditionally for non-string metadata can now raise if a user-supplied object has a failing __str__ implementation (e.g., proxy/mock/domain objects), which causes propagate_attributes to throw and break request/span execution. Before this change, non-string metadata was dropped without raising, so this is a regression in fault tolerance for malformed metadata inputs.

Useful? React with 👍 / 👎.

@hassiebp hassiebp self-requested a review March 25, 2026 14:45
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.

bug: propagate_attributes drops non-string metadata instead of coercing (breaks LangGraph integration)

2 participants