Skip to content

[bug] [langchain] JSON serialization fails with non-string dictionary keys (e.g., Azure PISubType enum) #2507

@seanlee10

Description

@seanlee10

Describe the bug
The LangChain tracer fails to serialize span attributes when dictionary keys are non-string types (such as enum values). The safe_json_dumps fallback only handles non-serializable values via default=str, but does not handle non-string dictionary keys, which causes json.dumps() to fail.

To Reproduce

  1. Use Azure AI Document Intelligence to process a document with PII detection enabled
  2. Pass the resulting document through a LangChain chain with openinference-instrumentation-langchain enabled
  3. The tracer fails when attempting to serialize metadata containing PISubType enum keys

Expected behavior
The tracer should successfully serialize objects with non-string dictionary keys by converting them to strings, rather than raising a TypeError.

Screenshots

2025-12-08 14:48:07.927 | ERROR | Failed to get attribute.
Traceback (most recent call last):
  File ".../openinference/instrumentation/langchain/_tracer.py", line 481, in _json_dumps
    return json.dumps(obj, cls=_OpenInferenceJSONEncoder, ensure_ascii=False)
  ...
TypeError: keys must be str, int, float, bool or None, not PISubType

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../openinference/instrumentation/langchain/_tracer.py", line 327, in wrapper
    yield from wrapped(*args, **kwargs)
  File ".../openinference/instrumentation/langchain/_tracer.py", line 421, in _convert_io
    json_value = _json_dumps(obj)
  File ".../openinference/instrumentation/langchain/_tracer.py", line 484, in _json_dumps
    return safe_json_dumps(obj)
  File ".../openinference/instrumentation/helpers.py", line 21, in safe_json_dumps
    return json.dumps(obj, default=str, ensure_ascii=False, **kwargs)
  ...
TypeError: keys must be str, int, float, bool or None, not PISubType

Desktop (please complete the following information):

  • OS: Linux
  • Python: 3.11

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions