Skip to content

Bug: datetime Object Not JSON Serializable in Tool Execution #137

@loukratz-bv

Description

@loukratz-bv

Summary

When streaming tool execution events, QType's chat converter attempts to serialize tool inputs as JSON, but fails when the input contains a datetime object.

Error

TypeError: Object of type datetime is not JSON serializable

Stack Trace

File "qtype/interpreter/stream/chat/converter.py", line 272, in _convert_tool_execution_start
    input_json = json.dumps(event.tool_input)
File "json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
File "json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type datetime is not JSON serializable

Root Cause

The flow passes a datetime object as a tool input. When QType tries to stream the tool execution start event, it attempts to serialize the tool inputs to JSON for logging/display purposes. Python's default JSON encoder cannot serialize datetime objects.

Impact

  • Prevents streaming of tool execution events
  • Breaks conversational interfaces that rely on SSE streaming
  • Occurs when datetime variables are passed to tools

Workaround

Instead of passing datetime objects to tools:

  1. Format datetime to string before passing to tool
  2. Create wrapper functions that handle date formatting internally
  3. Use only JSON-serializable types (str, int, float, bool, list, dict) as tool inputs

Resolution Options

  1. Application Level (Implemented): Create tool wrapper functions that accept only strings
  2. QType Framework Level: Update the chat converter to use a custom JSON encoder that handles datetime objects
  3. QType Framework Level: Convert datetime objects to ISO format strings before serialization

Example Fix

# Before (causes error)
def get_data(client: str, timestamp: datetime) -> str:
    ...

# After (works)
def get_data(client: str, date: str) -> str:
    # date should be pre-formatted as "YYYY-MM-DD"
    ...

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