Skip to content

Agent tool results that are Pydantic models, dataclasses, enums or datetimes are sent to the LLM as Python repr #13727

Closed

Description

@astroknot-sheep

Describe the bug
When a tool used by Agent returns a Pydantic model, a dataclass, an Enum, a set, a datetime/date, a UUID, a Decimal or a path (or a list/dict containing one), the tool result sent back to the LLM is a Python repr, not JSON. _result_to_string calls json.dumps on the result, it fails on the first such value, and the whole result falls back to str(). A warning is also logged on every call.

To Reproduce

import datetime
from enum import Enum
from pydantic import BaseModel
from haystack.components.agents.tool_calling import _result_to_string

class Status(Enum):
    OPEN = "open"

class Ticket(BaseModel):
    id: int
    status: Status

print(_result_to_string(Ticket(id=1, status=Status.OPEN)))
# id=1 status=<Status.OPEN: 'open'>
print(_result_to_string({"id": 1, "created": datetime.date(2026, 1, 2)}))
# {'id': 1, 'created': datetime.date(2026, 1, 2)}

The same happens end to end with a function tool that returns these values: the ToolCallResult text the model sees is the repr.

Expected behavior
{"id": 1, "status": "open"} and {"id": 1, "created": "2026-01-02"}, i.e. JSON, like for plain dicts and for objects that have to_dict().

Additional context
Returning a Pydantic model from a tool is quite common, and the reprs (<Status.OPEN: 'open'>, datetime.date(...)) are noisy for the model. I have a small fix with tests (a default= hook for json.dumps covering these types, still falling back to str() for anything else) and will open a PR.

System:

  • Haystack version: main (104a83f)
  • Python 3.13

I used an AI coding assistant to help find and write this up; I reproduced it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions