Skip to content

fix: preserve surrogate-containing metadata in OTLP - #2084

Open
DiadkoShmek wants to merge 2 commits into
langfuse:mainfrom
DiadkoShmek:fix/metadata-surrogate-encoding
Open

DiadkoShmek wants to merge 2 commits into
langfuse:mainfrom
DiadkoShmek:fix/metadata-surrogate-encoding

Conversation

@DiadkoShmek

@DiadkoShmek DiadkoShmek commented Oct 31, 2026 •

Copy link
Copy Markdown

Summary

Keep metadata values containing surrogate code points when encoding spans as OTLP. For example, a filename returned by os.fsdecode(b"file_\xff.txt") contains U+DCFF. ensure_ascii=False leaves it literally in the JSON string; protobuf rejects that invalid UTF-8 attribute, so the metadata key disappears while the span and sibling attributes survive.

After JSON serialization, escape surrogate code points while retaining ordinary Unicode characters, compact structured values, and the existing plain-string/EventSerializer dispatch. Both plain strings and nested values follow the same transport-safe path. This is a metadata-value fix; attribute names and other span fields are unchanged.

Verification

  • Actual SDK observation → finished in-memory span → OTLP encode_spans, with lone high/low surrogates, nested values, mixed Unicode/surrogates, and an ordinary Unicode control.
  • Before the fix at 1fc854dd2b241cf8e3dac38bfd5907ff2fe365e3: 4 failed, 1 passed, specifically because the affected metadata key is missing after OTLP encoding. Sibling metadata and the span are preserved.
  • uv run --frozen pytest tests/unit/test_otel.py -q: 102 passed, 2 skipped.
  • uv run --frozen ruff check .: passed.
  • uv run --frozen mypy langfuse --no-error-summary: passed.
  • Ruff formatting checks on both changed files and git diff --check: passed.
  • Python 3.12.8, OpenTelemetry SDK 1.45.1 / HTTP exporter 1.45.0, from uv sync --locked.

A separate AI review reran 13 focused OTLP/string-subclass/masking cases and checked protobuf byte roundtrips. Full unit, e2e and live-provider suites were not run; the tests above need no server or provider. No generated API files changed.

AI disclosure: Codex prepared and tested this change, with a separate AI review of the complete diff. No personal human code review is claimed.

RetriggerView in GreptileConfidence Score: 4/5

The metadata fix appears sound, but the new test's import must move before merging to satisfy the repository rule.

Summary

Escapes surrogate code points after metadata values become JSON, allowing those values to reach OTLP without escaping ordinary Unicode.

  • Metadata values with surrogates survive OTLP encoding.

Reviews (1) · Last reviewed commit: "fix: preserve surrogate-containing metad..." · Reviewed by Greptile

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

@CLAassistant

CLAassistant commented Oct 31, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread tests/unit/test_otel.py Resolved

This branch has not been deployed

No deployments
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