fix(client): skip string serialization in attribute values #1297
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Important
Fixes string serialization in
_serializefunction to prevent unnecessary JSON encoding for OpenTelemetry span attributes._serializeinattributes.pynow returns strings as-is, skipping JSON serialization for strings.test_decorators.pyandtest_openai.pyto expect unquoted string outputs.test_openai.pyto check for integer output instead of string.test_decorators.pyto match new serialization behavior.This description was created by
for 6e5d83f. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR modifies the
_serializefunction inlangfuse/_client/attributes.pyto fix Unicode escaping issues in attribute values. The change adds an early return condition that skips JSON serialization for string values, returning them as-is instead of double-serializing them.What Changed:
The
_serializefunction now checks if the input isNoneor already a string before applying JSON serialization. Previously, all non-None objects (including strings) were being processed throughjson.dumps()with theEventSerializerclass, which would escape Unicode characters and wrap strings in additional quotes.Why This Matters:
This change addresses a specific problem with OpenTelemetry span attributes, where string values should be stored directly without additional JSON encoding. The previous implementation was causing unnecessary double-serialization - strings were being JSON-encoded when they should have been passed through unchanged.
Integration with Codebase:
The
_serializefunction appears to be part of the client's attribute handling system, likely used when preparing data for OpenTelemetry spans or similar telemetry operations. TheEventSerializerclass (referenced in the test context) is still used for complex objects that genuinely need specialized serialization, but simple strings now bypass this processing entirely. This maintains the existing behavior for complex data types while optimizing the common case of string attributes.Confidence score: 4/5
langfuse/_client/attributes.pyto ensure the string type check covers all expected string types