-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Description
Bug Description
Description
When using the Tavily Search API component as an Agent tool, NaN values in the API response cause a PostgreSQL JSON parsing error, preventing the agent from functioning properly.
Error Message
Error: (psycopg.errors.InvalidTextRepresentation) invalid input syntax for type json
DETAIL: Token "NaN" is invalid.
CONTEXT: JSON data, line 1: ...ill remain dry throughout the day.", "title": null...
unnamed portal parameter $4 = '...'
[SQL: UPDATE message SET timestamp=%(timestamp)s::TIMESTAMP WITHOUT TIME ZONE, text=%(text)s::VARCHAR, properties=%(properties)s::JSON, content_blocks=%(content_blocks)s::JSON WHERE message.id = %(message_id)s::UUID]
Root Cause
The Tavily API returns NaN values in numeric fields (e.g., temperature, probability), which are not valid in PostgreSQL's JSON type. Python's float('nan') cannot be serialized to valid JSON for database storage.
Temporary Fix
Sanitize NaN values before returning the DataFrame by converting them to None.
This resolves the immediate error, but a more efficient solution may exist.
def fetch_content_dataframe(self) -> DataFrame:
import pandas as pd
def replace_nan(obj):
"""Recursively replace NaN with None"""
if isinstance(obj, dict):
return {k: replace_nan(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [replace_nan(v) for v in obj]
elif pd.isna(obj):
return None
return obj
data = self.fetch_content()
df = DataFrame(data)
# Replace NaN values
for col in df.columns:
df[col] = df[col].apply(replace_nan)
return dfEnvironment
- PostgreSQL version: 16
Reproduction
Steps to Reproduce
- Add Tavily Search API component to a flow
- Connect it to an Agent as a tool
- Execute a search query through the agent
- PostgreSQL throws an
InvalidTextRepresentationerror when attempting to store the result
Expected behavior
Expected Behavior
The Tavily Search API component should work seamlessly with Agent tools,
and search results should be stored in PostgreSQL without errors.
Actual Behavior
PostgreSQL throws a JSON parsing error due to NaN values in the response.
Who can help?
No response
Operating System
Ubuntu 24.04.3 LTS
Langflow Version
1.7.2
Python Version
None
Screenshot
No response
Flow File
No response