Function tools pass raw LLM JSON to the function: Pydantic/dataclass parameters arrive as dicts and Literal/int arguments are never checked

オープン
#13,659 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

  • #13661 @ravismash による — マージされずにクローズ
  • #13662 @Luckyou-d による — マージされずにクローズ

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
15/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python

調査の方向性

Start by reading create_tool_from_function, Tool.invoke, and the Agent’s _prepare_tool_args path; compare them with ComponentTool’s _convert_param handling. Check the sync and async invocation paths and tests for function tools with Pydantic models, dataclasses, Literal, and int parameters. Done means arguments are validated and converted before the function runs, with validation errors returned through ToolInvocationError.

索引モデルが issue の本文から書いたものです。

説明

handled internally P2

Describe the bug
create_tool_from_function / @tool builds the tool's JSON schema from the function signature through a Pydantic model, but Tool.invoke then calls the function with the LLM's parsed JSON as-is. The Agent path (_prepare_tool_args -> tool.invoke(**args)) doesn't convert or validate either. ComponentTool converts its inputs (_convert_param, TypeAdapter), but function tools don't.

Two consequences:

  1. A parameter typed as a Pydantic model or dataclass (or list[Model]) receives a plain dict, so the call fails even when the LLM sent arguments that match the advertised schema exactly (the schema even contains $defs/Item).
  2. Arguments that violate the tool's own schema are executed silently: Literal["low", "high"] accepts "LOUD", int accepts "3". The tool runs with values its schema forbids, and the agent never gets an error it could correct.

Error message

ToolInvocationError: Failed to invoke Tool `place_order` with parameters {'items': [{'sku': 'A', 'qty': 2}], 'currency': 'USD', 'priority': 1}. Error: 'dict' object has no attribute 'qty'

For case 2 there is no error: set_level returns level='LOUD' retries='3'.

Expected behavior
Function tools validate and convert arguments with the same Pydantic model that produced their schema before calling the function: model/dataclass parameters arrive as instances, and invalid arguments raise a ToolInvocationError carrying the validation message, so the LLM can retry with corrected arguments.

Additional context
Possible fix: keep the model created in create_tool_from_function and validate the LLM-provided arguments with it at invoke time (sync and async), passing validated field values to the function. Values injected from inputs_from_state and State-annotated parameters would be left untouched. I'm happy to open a PR with tests.

To Reproduce

from typing import Literal
from pydantic import BaseModel
from haystack.tools import tool

class Item(BaseModel):
    sku: str
    qty: int

@tool
def place_order(items: list[Item], currency: Literal["USD", "EUR"], priority: int) -> str:
    """Place an order."""
    return f"{sum(i.qty for i in items)} items in {currency}"

# arguments exactly as an LLM returns them, matching place_order.parameters
place_order.invoke(items=[{"sku": "A", "qty": 2}], currency="USD", priority=1)
# -> ToolInvocationError: ... 'dict' object has no attribute 'qty'

@tool
def set_level(level: Literal["low", "high"], retries: int) -> str:
    """Set level."""
    return f"level={level!r} retries={retries!r}"

print(set_level.invoke(level="LOUD", retries="3"))
# -> level='LOUD' retries='3'

FAQ Check

System:

  • OS: Linux
  • Haystack version: 3.4.1 (also present on main)

👋 Hello there! This issue will be handled internally and isn't open for external contributions. If you'd like to contribute, please take a look at issues labeled contributions welcome or good first issue. We'd really appreciate it!

主要言語
Python
スター
27.1k
フォーク
3.5k
平均マージ
1日 8時間
マージ済み PR(30日)
239

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

deepset-ai/haystack のほかの issue

deepset-ai/haystack の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。