TypeError: Object of type NAType is not serializable during state serialization in LangChain #29082
Open
5 tasks done
Labels
🤖:bug
Related to a bug, vulnerability, unexpected error with an existing feature
Checked other resources
Example Code
import pandas as pd
import numpy as np
import json
def debug_state(state):
"""Debug and identify keys with non-serializable data."""
for key, value in state.items():
try:
json.dumps(value) # Check JSON serialization
except TypeError as e:
print(f"Key '{key}' contains non-serializable data: {e}")
Example Function
def final_return_node(state):
"""Simulate LangChain state serialization."""
print("--- Simulating LangChain State Serialization ---")
Simulate State and Call Function
state = {}
try:
final_return_node(state)
except Exception as e:
print("Error:", str(e))
Error Message and Stack Trace (if applicable)
--- Simulating LangChain State Serialization ---
Debugging state for non-serializable data...
Key 'working_data' contains non-serializable data: Object of type NAType is not JSON serializable
JSON Serialization Error: Object of type NAType is not JSON serializable
Traceback (most recent call last):
File "example.py", line 40, in
final_return_node(state)
File "example.py", line 33, in final_return_node
json.dumps(state) # Validate JSON serialization
File "C:\Python\lib\json_init_.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Python\lib\json\encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Python\lib\json\encoder.py", line 258, in iterencode
return _iterencode(o, 0)
File "C:\Python\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.class.name} '
TypeError: Object of type NAType is not JSON serializable
Description
Description
I am encountering a persistent issue in LangChain where attempting to serialize a state containing a pandas DataFrame with
pd.NA
values results in aTypeError: Object of type NAType is not JSON serializable
. This error occurs despite implementing various sanitization techniques to replace or handle non-serializable values.What I'm Doing
final_return_node
) in a LangChain graph that processes a pandas DataFrame (working_data
) and updates the graph’s state with the processed data.to_dict(orient="records")
.What I Expect to Happen
pd.NA
andnp.nan
are sanitized and replaced with JSON-serializable alternatives (None
, strings, etc.).What Is Actually Happening
pd.NA
andnp.nan
withNone
, and validating the state using bothjson.dumps
andmsgpack.packb
, LangChain still raises aTypeError: Object of type NAType is not serializable
.pd.NA
values somewhere within the state or during serialization.Steps Taken to Debug
pd.NA
andnp.nan
in the DataFrame using:Hypothesis
This bug is blocking my ability to process state updates and proceed through the LangChain graph workflow. It seems specific to LangChain's serialization implementation, as the sanitized state passes JSON and MsgPack validation outside of LangChain.
System Info
python -m langchain_core.sys_info
System Information
Package Information
Optional packages not installed
Other Dependencies
The text was updated successfully, but these errors were encountered: