Skip to content

fix: convert common Python types in Agent tool results to JSON - #13728

Merged
sjrl merged 3 commits into
deepset-ai:mainfrom
astroknot-sheep:fix/tool-result-json-common-types
Nov 16, 2026
Merged

sjrl merged 3 commits into
deepset-ai:mainfrom
astroknot-sheep:fix/tool-result-json-common-types

Conversation

@astroknot-sheep

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

_result_to_string serializes tool results with json.dumps, and when that fails the whole result falls back to str(). So a tool returning a Pydantic model, dataclass, Enum, set, datetime/date/time, UUID, Decimal or path (also when nested in a list/dict) sent a Python repr to the LLM, e.g. id=1 status=<Status.OPEN: 'open'> or {'created': datetime.date(2026, 1, 2)}, and logged a warning on every call.

I added a small _json_default hook passed as default= to json.dumps:

  • Pydantic models -> model_dump(mode="json")
  • dataclasses -> dict of their fields (nested values go through the same hook)
  • Enum -> .value, set/frozenset -> list
  • datetime/date/time -> ISO format, UUID/Decimal/paths -> str

Anything else still raises TypeError, so the existing str() fallback and warning are unchanged for truly unsupported types. Objects with to_dict() (Document, ChatMessage, ...) are still handled by _serializable_value first.

How did you test it?

  • New test_result_to_string_with_common_python_types (fails on main, passes with the fix) and test_result_to_string_falls_back_to_str_for_unsupported_types.
  • pytest -m "not integration" test/components/agents test/tools (645 passed, 2 skipped; test_component_tool.py skipped locally because the flaky plugin isn't installed in my env).
  • ruff format/check and mypy clean on the changed files.

Notes for the reviewer

Release note added under fixes. I used an AI coding assistant to help find and write this fix; I reproduced the bug, reviewed the change and ran the tests myself.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

@astroknot-sheep
astroknot-sheep requested a review from a team as a code owner November 13, 2026 00:38
@astroknot-sheep
astroknot-sheep requested review from davidsbatista and removed request for a team November 13, 2026 00:38
@vercel

vercel Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

@astroknot-sheep is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Nov 13, 2026
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/agents
  tool_calling.py
Project Total  

This report was generated by python-coverage-comment-action

@sjrl
sjrl requested review from sjrl and removed request for davidsbatista November 13, 2026 07:10
Comment on lines +854 to +857
"path": Path("/tmp/a.txt"),
}
)
) == {"color": "red", "tags": ["a"], "day": "2026-01-02", "price": "1.50", "path": "/tmp/a.txt"}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails on Windows because str(Path("/tmp/a.txt")) is \tmp\a.txt there. Could we use a path without separators?

Suggested change
"path": Path("/tmp/a.txt"),
}
)
) == {"color": "red", "tags": ["a"], "day": "2026-01-02", "price": "1.50", "path": "/tmp/a.txt"}
"path": Path("a.txt"),
}
)
) == {"color": "red", "tags": ["a"], "day": "2026-01-02", "price": "1.50", "path": "a.txt"}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that. Done in 9fe49eb, the test now uses Path("a.txt").

from concurrent.futures import ThreadPoolExecutor
from typing import Any

from pydantic_core import to_jsonable_python

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we import pydantic_core directly now, could we also declare it in the dependencies in pyproject.toml? We do the same for MarkupSafe, e.g. "pydantic-core", # already required by pydantic but used directly in tool_calling._result_to_string

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added "pydantic-core" to the dependencies in pyproject.toml with the same kind of comment as MarkupSafe (9fe49eb).

@sjrl

sjrl commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Hey @astroknot-sheep pinging to ask if you have time to work on this

@astroknot-sheep

Copy link
Copy Markdown
Contributor Author

Hi @sjrl, sorry for the delay. I have pushed both changes in 9fe49eb (separator-free Path in the test, and pydantic-core declared in pyproject.toml). The tool_calling tests pass locally.

@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedpypi/​pydantic-core@​2.50.4100100100100100

View full report

@sjrl sjrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sjrl
sjrl merged commit b22ff0a into deepset-ai:main Nov 16, 2026
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants