fix: coerce non-string metadata values instead of dropping them#1575
fix: coerce non-string metadata values instead of dropping them#1575ChunkyTortoise wants to merge 1 commit intolangfuse:mainfrom
Conversation
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
|
|
There was a problem hiding this comment.
💡 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 |
There was a problem hiding this comment.
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 👍 / 👎.
Summary
str()in_propagate_attributesinstead of dropping them with a warninglanggraph_step(int),langgraph_triggers(list),langgraph_path(tuple) into metadata on every graph stepChanges
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 typesTest plan
test_non_string_metadata_values_coercedverifies int/list/tuple metadata is coerced to stringsFixes #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 valueguard in_propagate_attributesensures 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 testtest_non_string_metadata_values_coercedvalidates all three LangGraph metadata types (int, list, tuple) as well as string pass-through.Minor follow-ups worth addressing:
propagate_attributessignature still declaresmetadata: Optional[Dict[str, str]]; updating it toOptional[Dict[str, Any]]would prevent spurious type-checker errors for callers passing LangGraph metadata.Confidence Score: 5/5
Important Files Changed
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"]Reviews (1): Last reviewed commit: "fix: coerce non-string metadata values i..." | Re-trigger Greptile