Skip to content

Agent run aborts instead of falling back to str() when a tool result's to_dict fails #13782

Open

Description

@Dengzi555

Describe the bug
Agent tool results are converted to a string with _result_to_string, whose docstring promises a str() fallback "if serialization fails". The _serializable_value call sits outside the try block, so when the tool result object has a to_dict attribute that raises, is not callable, or requires arguments, the exception escapes the fallback and aborts the whole run.

In _make_context_bound_invoke/_make_bounded_invoke_async the tool call itself is wrapped in a try that only catches ToolInvocationError, and futures[idx].result() re-raises anything else. So a tool returning such an object kills the Agent run even with raise_on_failure=False, while a tool that itself raises ToolInvocationError is handled gracefully.

Error message
With a tool returning an object whose to_dict raises:

ValueError: boom

raised from haystack/tracing/utils.py (value.to_dict(), line 67) through _result_to_string → _process_tool_output → _build_tool_result_message → future.result(), instead of the documented warning + str(result) fallback.

Non-callable to_dict and to_dict(self, required_arg) produce the equivalent TypeErrors on the same path.

Expected behavior
_result_to_string falls back to str() conversion and logs the existing "Tool result is not JSON serializable. Falling back to str conversion." warning, for any serialization failure — including failures of the _serializable_value step, exactly like json.dumps failures already do (coerce_tag_value in tracing/utils.py wraps its _serializable_value call in try/except for the same reason).

To Reproduce

from haystack.components.agents.tool_calling import _result_to_string

class BrokenResult:
    def to_dict(self):
        raise ValueError("boom")
    def __str__(self):
        return "<broken>"

_result_to_string(BrokenResult())   # raises ValueError instead of returning "<broken>"

FAQ Check

  • Have you had a look at our new FAQ page?

System:

  • OS: Windows
  • Haystack version: main (3.6.0-rc0)

I have a fix ready and will open a PR that closes this issue.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium priority, add to the next sprint if no P1 available

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions