Skip to content

## Bug: NaN values in Tavily Search API output cause PostgreSQL JSON error when used as Agent tool #11324

@kend3b

Description

@kend3b

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 df

Environment

  • PostgreSQL version: 16

Reproduction

Steps to Reproduce

  1. Add Tavily Search API component to a flow
  2. Connect it to an Agent as a tool
  3. Execute a search query through the agent
  4. PostgreSQL throws an InvalidTextRepresentation error 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions