Reputation: 23
I'm encountering a TypeError: Object of type NAType is not serializable
error while working with LangGraph. This issue arises when invoking the graph, specifically during the return process of a node function. The error persists even when I attempt to return a simple dictionary like {"final_return": "This works"}
.
I have a node function (final_return_node
) that:
state
object.state
dictionary with the processed DataFrame using to_dict(orient="records")
.Additionally, I'm using the "Human-in-the-Loop" command interrupt feature from LangGraph, which allows manual interventions during the node execution.
import pandas as pd
import numpy as np
import json
from collections.abc import Mapping
def sanitize_state(obj):
"""
Recursively sanitize the state object to ensure all components are JSON-serializable.
"""
if isinstance(obj, Mapping):
return {sanitize_state(key): sanitize_state(value) for key, value in obj.items()}
elif isinstance(obj, (list, tuple, set)):
return [sanitize_state(item) for item in obj]
elif isinstance(obj, pd.DataFrame):
return sanitize_state(obj.to_dict(orient="records"))
elif isinstance(obj, pd.Series):
return sanitize_state(obj.tolist())
elif obj is pd.NA or (isinstance(obj, float) and np.isnan(obj)) or obj is None:
return None
elif isinstance(obj, (int, float, str, bool)):
return obj
elif isinstance(obj, (pd.Timestamp, pd.Timedelta)):
return obj.isoformat() if hasattr(obj, 'isoformat') else str(obj)
elif isinstance(obj, (bytes, bytearray)):
import base64
return base64.b64encode(obj).decode('utf-8')
else:
return str(obj)
def is_json_serializable(obj):
"""
Check if the object is JSON-serializable.
"""
try:
json.dumps(obj)
return True
except (TypeError, OverflowError):
return False
def sanitize_and_verify_state(state):
"""
Sanitize the state object and verify it's JSON-serializable.
"""
sanitized = sanitize_state(state)
if not is_json_serializable(sanitized):
raise ValueError("Sanitized state is not JSON-serializable.")
return sanitized
def final_return_node(self, state):
print("---MAKING FINAL RETURN---")
state_dict = state.get("keys", {})
working_data = state_dict.get("working_data", [])
cmm_input_8 = pd.DataFrame(working_data)
sanitized_df = cmm_input_8.fillna("").replace({pd.NA: None, np.nan: None})
state["keys"].update({
"final_return": sanitized_df.to_dict(orient="records"),
})
print("Completed Final Return, Sanitizing State...")
state = sanitize_and_verify_state(state)
print("Sanitized State Successfully.")
return {"final_return": "This works"}
{"final_return": "This works"}
, I receive the following error:TypeError Traceback (most recent call last)
...
TypeError: Object of type NAType is not serializable
Returning a Static Dictionary
Replaced the function's return with return {"final_return": "This works"}
.
Result: Error persists.
Replacing Missing Values in the DataFrame
Used fillna("")
to replace all missing values with blanks.
Result: Issue still occurs.
Inspecting the State
Verified that the state object processes fine during execution.
Observation: Error arises at the return statement despite sanitizing the DataFrame.
Simplifying the State Update
Avoided updating the state and returned a static dictionary directly.
Result: Error still persists.
The issue seems to be unrelated to the state or DataFrame itself since the graph executes and processes correctly. It appears to be tied to the return statement and how LangGraph handles the returned data, possibly attempting to serialize the entire state object, which might still contain non-serializable pd.NA
values.
{
'keys': {
'original_user_input': 'make cmm input file',
'uploaded_data': 'BatchSample_USA_Test.csv',
'uploaded_codebook': None,
'chat_history': ['Hello, how can I assist?'],
'node': None,
'working_data': [
{
'Name': 'atch Sample',
'GroupName': 'SLSP',
'GroupDescription': 'Property',
'AnalysisDate': '09/01/2024',
'Loan': 'Loan AAA',
'Description': '',
'Origination': '09/01/2025',
'Maturity': '09/01/2033',
'Ownership': nan, # numpy.nan
'CurrentOutstandingLoanBalance': 5000000.0,
},
# ... more records
],
# ... other keys
},
# ... other sections
}
What could be causing LangGraph to raise a TypeError: Object of type NAType is not serializable
during a simple dictionary return? Is it an internal LangGraph serialization issue or a misconfiguration on my part? How can I effectively return the entire state to ensure all components are JSON-serializable?
Any assistance would be greatly appreciated!
Upvotes: 0
Views: 64