haystack icon indicating copy to clipboard operation
haystack copied to clipboard

Tool rejects inputs_from_state arguments accepted through **kwargs

Open skulitom opened this issue 1 month ago • 3 comments

Describe the bug

Tool rejects an inputs_from_state mapping when its target argument is accepted through **kwargs but is absent from the JSON schema. This prevents an agent from injecting state-only context into a keyword-accepting tool without exposing that argument in the model-facing schema.

The problem affects both synchronous functions and native async functions. It reproduces on current main, 76fc2d3d7492ec243eed40eb939a836f2a8b29da (3.5.0-rc0).

To reproduce

from haystack.tools import Tool


def search(query: str, **context: str) -> str:
    return f"{query} for {context['tenant']}"


tool = Tool(
    name="search",
    description="Search the current tenant's documents",
    parameters={
        "type": "object",
        "properties": {"query": {"type": "string"}},
        "required": ["query"],
    },
    function=search,
    inputs_from_state={"tenant_id": "tenant"},
)

assert tool.invoke(query="invoices", tenant="acme") == "invoices for acme"

Actual behavior

Construction raises:

ValueError: inputs_from_state maps 'tenant_id' to unknown parameter 'tenant'. Valid parameters are: {'query', 'context'}.

The order of the names in the error's set may vary.

Expected behavior

Construction succeeds because search accepts tenant through **context. An Agent configured with state_schema={"tenant_id": {"type": str}} can supply that state to the tool while the model-facing schema contains only query.

Additional context

Tool._get_valid_inputs() currently treats the names returned by inspect.signature() as an exhaustive set, even when the signature includes inspect.Parameter.VAR_KEYWORD. Ordinary explicit signatures should continue to reject unknown mapping targets, and mapping values should still be required to be strings.

Local verification reproduced four failing cases before the fix (sync/native async, each with empty/partial schemas). With a focused fix, those cases and the surrounding tool/agent suites pass. A separate public-API reproduction also passes Agent.run, Agent.run_async with a sync tool, and Agent.run_async with a native async tool, using MockChatGenerator without a live provider.

System

  • Ubuntu 22.04 under WSL2
  • Python 3.12.15
  • Haystack source at the main commit above
  • Hatch 1.18.1

AI disclosure: Codex discovered and reproduced this issue and prepared the proposed fix and tests.

skulitom avatar Oct 23 '26 15:10 skulitom

In general, you can freely create the function behind your Tool declaring parameters explicitly (or create a small wrapper if needed). So I am not totally convinced that supporting **kwargs is necessary/a good idea. Could you provide a real-world use case where this is needed?

anakin87 avatar Oct 26 '26 14:10 anakin87

Thanks — I agree that an explicit signature is clearer for a hand-written tool, and a small wrapper works for the original example.

A concrete SDK example is Boto3's Bedrock retrieve operation. Its generated Python callable has the signature (*args, **kwargs). An agent can supply retrievalQuery through the model-facing schema while taking knowledgeBaseId from conversation state:

retrieval_tool = Tool(
    name="retrieve_docs",
    description="Retrieve documents from the selected knowledge base",
    function=bedrock_client.retrieve,
    parameters={
        "type": "object",
        "properties": {
            "retrievalQuery": {
                "type": "object",
                "properties": {"text": {"type": "string"}},
                "required": ["text"],
            }
        },
        "required": ["retrievalQuery"],
    },
    inputs_from_state={"knowledge_base": "knowledgeBaseId"},
)

I reproduced this locally with Boto3 1.43.118 and botocore.stub.Stubber, without making AWS requests. The current validation rejects knowledgeBaseId during construction. With the proposed validation change, both Agent.run and Agent.run_async deliver the query and state-supplied ID to the SDK. I also verified that an explicit wrapper works without the change.

The benefit would be for an adapter that registers SDK operations from configuration: it could reuse the existing callables instead of maintaining a separate Python wrapper for each operation and its state-supplied parameters. I don't have a production deployment report for this; it is a verified integration example. The tradeoff is that a **kwargs callable cannot provide name-level validation, so an SDK such as Boto3 catches unknown parameters later. If explicit signatures are the intended contract for these adapters too, I'm happy to follow that policy.

skulitom avatar Oct 26 '26 14:10 skulitom

Thank you for sharing this example. I think a @tool wrapper would be a simpler approach:

from haystack.tools import tool

@tool(inputs_from_state={"knowledge_base": "knowledge_base_id"})
def retrieve_docs(query: str, knowledge_base_id: str):
    """Retrieve documents from the selected knowledge base."""
    return bedrock_client.retrieve(
        retrievalQuery={"text": query},
        knowledgeBaseId=knowledge_base_id,
    )

I'm closing this issue for now, but I am happy to revisit this decision if a use case emerges where available solutions would be impractical.

anakin87 avatar Oct 27 '26 14:10 anakin87