Skip to content

fix: preserve non-ASCII characters in JSON serialization#1512

Closed
edwardpwtsoi wants to merge 2 commits intolangfuse:mainfrom
edwardpwtsoi:main
Closed

fix: preserve non-ASCII characters in JSON serialization#1512
edwardpwtsoi wants to merge 2 commits intolangfuse:mainfrom
edwardpwtsoi:main

Conversation

@edwardpwtsoi
Copy link

@edwardpwtsoi edwardpwtsoi commented Feb 5, 2026

Add ensure_ascii=False to json.dumps calls to prevent Chinese, Japanese,
and other non-ASCII characters from being escaped as \uXXXX sequences.

This fixes the display of non-ASCII text in trace list views, where
characters were showing as Unicode escape sequences instead of
readable text.


Important

Add ensure_ascii=False to JSON serialization in utils.py and serializer.py to preserve non-ASCII characters.

  • Behavior:
    • Add ensure_ascii=False to json.dumps in span_formatter() in utils.py to prevent non-ASCII characters from being escaped.
    • Add ensure_ascii=False to super().encode() in encode() in serializer.py to handle non-ASCII characters correctly.
  • Misc:
    • Fixes display of non-ASCII text in trace list views by preventing Unicode escape sequences.

This description was created by Ellipsis for 0a38cb5. You can customize this summary. It will automatically update as commits are pushed.

Disclaimer: Experimental PR review

Greptile Overview

Greptile Summary

This PR aims to preserve non-ASCII characters (Chinese, Japanese, etc.) in JSON serialization by adding ensure_ascii=False to prevent Unicode escape sequences.

Critical Issue Found:

  • langfuse/_utils/serializer.py:176 contains a syntax error that will cause TypeError at runtime
  • JSONEncoder.encode() does not accept ensure_ascii as a parameter - it must be set in the constructor instead
  • The change in langfuse/_client/utils.py:62 is correct

Impact:

  • All code paths using EventSerializer().encode() will fail with TypeError
  • This affects request serialization (langfuse/_utils/request.py:63), score ingestion (langfuse/_task_manager/score_ingestion_consumer.py:88, 120), and attribute conversion (langfuse/_client/attributes.py:177)

Required Fix:
Set ensure_ascii=False in EventSerializer.__init__() instead of passing it to super().encode()

Confidence Score: 0/5

  • This PR cannot be merged - it contains a critical syntax error that will cause runtime failures
  • The change in langfuse/_utils/serializer.py:176 passes ensure_ascii=False to super().encode(), but Python's JSONEncoder.encode() method only accepts one parameter (the object to encode). This will cause a TypeError: encode() takes 2 positional arguments but 3 were given when the code runs
  • langfuse/_utils/serializer.py requires immediate correction to prevent runtime errors

Important Files Changed

Filename Overview
langfuse/_client/utils.py Added ensure_ascii=False to json.dumps() call in span_formatter() to preserve non-ASCII characters
langfuse/_utils/serializer.py Attempted to pass ensure_ascii=False to super().encode() but this parameter is not accepted by the method - will cause TypeError

Sequence Diagram

sequenceDiagram
    participant Client
    participant json.dumps()
    participant EventSerializer
    participant JSONEncoder
    
    Note over Client,JSONEncoder: Two usage patterns
    
    rect rgb(200, 220, 240)
    Note over Client,EventSerializer: Pattern 1: Using json.dumps with cls parameter
    Client->>json.dumps(): json.dumps(data, cls=EventSerializer)
    json.dumps()->>EventSerializer: __init__() with ensure_ascii
    EventSerializer->>JSONEncoder: super().__init__(ensure_ascii=False)
    json.dumps()->>EventSerializer: default(obj)
    EventSerializer-->>json.dumps(): processed obj
    json.dumps()-->>Client: JSON string with non-ASCII chars
    end
    
    rect rgb(240, 220, 200)
    Note over Client,EventSerializer: Pattern 2: Direct instantiation
    Client->>EventSerializer: serializer = EventSerializer()
    EventSerializer->>JSONEncoder: super().__init__(ensure_ascii=False)
    Client->>EventSerializer: serializer.encode(obj)
    EventSerializer->>EventSerializer: self.default(obj)
    EventSerializer->>JSONEncoder: super().encode(processed_obj)
    Note over EventSerializer,JSONEncoder: BUG: ensure_ascii passed here (line 176)<br/>but encode() doesn't accept parameters
    JSONEncoder-->>EventSerializer: JSON string
    EventSerializer-->>Client: JSON string
    end
Loading

(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

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.

1 participant