Skip to content

ComponentTool executes dataclass default factories during schema generation #13689

Open

Description

@Elon-Jiang

Describe the bug

Constructing ComponentTool executes dataclass default_factory functions while generating the JSON schema. Merely describing a component can therefore consume IDs or perform setup intended for a real invocation. A factory whose runtime resource is not ready prevents tool construction, even when callers would supply the field explicitly.

_dataclass_to_pydantic_model calls field.default_factory() and stores the resulting value as a static Pydantic default. Keeping it as Field(default_factory=...) would describe the optional field without executing the factory.

To Reproduce

from dataclasses import dataclass, field
from haystack import component
from haystack.tools import ComponentTool

calls = []
def next_token():
    calls.append(True)
    return str(len(calls))

@dataclass
class Options:
    token: str = field(default_factory=next_token)

@component
class Echo:
    """Echo the token supplied by the options."""
    @component.output_types(token=str)
    def run(self, options: Options):
        return {"token": options.token}

tool = ComponentTool(component=Echo())
print(len(calls))                  # actual: 1; expected: 0
print(tool.invoke(options={}))    # actual: {'token': '2'}; expected: {'token': '1'}

Expected behavior

Schema construction should not run dataclass factories. Omitted values should be generated per invocation; explicit values should not run the factory. Schema generation should not advertise a concrete default that was created during setup.

Additional context

Reproduced through the real public component/tool APIs using Hatch, with no model/provider or external-service fixture. Three new regression cases fail on the original source, while two existing dataclass controls pass. Existing #13516 handles string annotations in the same helper and retains the factory call; this is a separate behavior defect. Preparing a focused fix and regression tests.

AI assistance: OpenAI Codex performed the investigation and local reproduction. No production incident or personal testing by the account owner is claimed.

System:

  • OS: macOS, Python 3.12.13
  • Haystack version: 3.6.0-rc0, main 3a753721265dc167bc908b947d7d33e7fead76f5

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions