OpenBB-finance/OpenBB · error · ValueError
Failed to convert DataFrame to JSON
Error message
Failed to convert DataFrame to JSON
What it means
Thrown by the OmniWidgetResponseModel model_validator when content is a pandas DataFrame and DataFrame.to_json(orient='records') fails (or its output cannot be re-parsed with json.loads). The validator converts tabular data into a list of records for the widget layer; any serialization failure is re-raised as this ValueError.
Source
Thrown at openbb_platform/extensions/platform_api/openbb_platform_api/response_models.py:252
if content.__class__.__name__ == "Figure":
values.parse_as = "chart"
try:
content = content.to_json()
except Exception as e:
raise ValueError("Failed to convert chart to JSON") from e
values.content = content
elif isinstance(content, dict) and "layout" in content and "data" in content:
values.parse_as = "chart"
elif isinstance(content, list) and all(
isinstance(item, dict) for item in content
):
values.parse_as = "table"
elif isinstance(content, pd.DataFrame):
values.parse_as = "table"
try:
content = json.loads(content.to_json(orient="records"))
except Exception as e:
raise ValueError("Failed to convert DataFrame to JSON") from e
values.content = content
elif isinstance(content, dict) and all(
isinstance(v, list) for v in content.values()
):
values.parse_as = "table"
try:
df = pd.DataFrame(content)
content = json.loads(df.to_json(orient="records"))
except Exception as e:
raise ValueError(
"Failed to convert dictionary of lists to list of records"
) from e
values.content = content
elif isinstance(content, str) and content.strip(): # pylint: disable=R0916
try:
content = json.loads(content)
except json.JSONDecodeError:
# Remove trailing commas in objects and arraysView on GitHub (pinned to 3e071fcc2c)
Solutions
- Inspect e.__cause__ from the raised ValueError to find the offending column/value.
- Convert problem columns before constructing the model: df = df.convert_dtypes() or df = df.astype(object).where(df.notna(), None).
- Pass the records yourself: content=df.to_json(orient='records') as a pre-serialized string, or content=json.loads(df.to_json(orient='records')).
- Drop or stringify non-serializable object columns (e.g. columns holding nested Data models).
Example fix
# before resp = OmniWidgetResponseModel(content=df_with_object_cols) # after records = json.loads(df.to_json(orient="records")) resp = OmniWidgetResponseModel(content=records)
Defensive patterns
Strategy: validation
Validate before calling
records = json.loads(df.to_json(orient="records")) resp = OmniWidgetResponseModel(content=records)
Type guard
import pandas as pd
def is_serializable_dataframe(obj) -> bool:
if not isinstance(obj, pd.DataFrame):
return False
try:
obj.to_json(orient="records")
return True
except Exception:
return False Try / catch
try:
resp = OmniWidgetResponseModel(content=df)
except ValueError as e:
if "DataFrame to JSON" in str(e):
df = df.convert_dtypes()
resp = OmniWidgetResponseModel(content=json.loads(df.to_json(orient="records")))
else:
raise Prevention
- Convert object columns to primitives before widget construction
- Test df.to_json() in unit tests for provider DataFrames
- Use .to_df() outputs which are already JSON-safe
When it happens
Trigger: Constructing OmniWidgetResponseModel(content=df) where df contains values pandas to_json cannot encode, or where the DataFrame uses non-string-MAX pathologies (e.g. mixed-type object columns with custom classes, Decimal, or unhashable objects) that break the JSON encoder.
Common situations: Handing a raw provider response DataFrame containing Data/BaseModel objects in cells; DataFrames built from records with numpy scalars in object columns; pandas version differences in to_json handling of NaN and mixed dtypes.
Related errors
- Failed to convert chart to JSON
- Failed to convert dictionary of lists to list of records
- At least one extension type must be selected.
- Incorrect email or password
- Charting is not installed. Please install `openbb-charting`.
AI-assisted analysis of OpenBB-finance/OpenBB@3e071fcc2c (2026-08-14).
Data as JSON: /api/errors/67fe8f41394a456d.
Report an issue: GitHub.