Function tools pass raw LLM JSON to the function: Pydantic/dataclass parameters arrive as dicts and Literal/int arguments are never checked
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 15/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- python
- Lĩnh vực
- ai, backend-api-design
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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:
- A parameter typed as a Pydantic model or dataclass (or
list[Model]) receives a plaindict, so the call fails even when the LLM sent arguments that match the advertised schema exactly (the schema even contains$defs/Item). - Arguments that violate the tool's own schema are executed silently:
Literal["low", "high"]accepts"LOUD",intaccepts"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
- Have you had a look at our new FAQ page?
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!
- Ngôn ngữ chính
- Python
- Star
- 27.1k
- Fork
- 3.5k
- Merge trung bình
- 1 ngày 8 giờ
- Pull request đã merge (30 ngày)
- 239
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của deepset-ai/haystack
-
BlockingConfirmationStrategy ignores an empty argument replacementCó thể đã có người làm @Aarkin7 đã nhận 3 ngày trước. Đang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 82/100
deepset-ai/haystack#13863 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
deepset-ai/haystack#13830 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
DocumentSplitter misplaces repeated literal chunks from an overlapping custom splitterCó thể đã có người làm @davidsbatista đã nhận 7 ngày trước. Đang mởP2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
deepset-ai/haystack#13806 · 2 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
CSVToDocument accepts an unsupported conversion_mode and silently converts in row modeCó thể đã có người làm @Lesereingrape đã nhận 54 ngày trước. Đang mởP3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
deepset-ai/haystack#13070 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
InMemoryDocumentStore: count_unique_metadata_by_filter and get_metadata_field_unique_values disagree on int/float/bool valuesCó thể đã có người làm @sjrl đã nhận 14 ngày trước. Đang mởP3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
deepset-ai/haystack#13040 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của deepset-ai/haystack
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
micronaut-projects/micronaut-core#14585 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
langgenius/graphon#443 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG]: Management endpoints /v1/memory/stats and /v1/memory/counts bypass API key authenticationĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
docling-project/docling-serve#788 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Mend: dependency security vulnerability untriaged
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
opensearch-project/cross-cluster-replication#1805 ·
Maintainer thường phản hồi trong vòng 1 ngày