Skip to content

Conversation

@vblagoje
Copy link
Member

@vblagoje vblagoje commented Dec 17, 2025

Why

Prevents runtime errors from typos in inputs_from_state and outputs_to_state by validating parameter/output names at tool construction time.

What

  • Added _get_valid_inputs() and _get_valid_outputs() methods to base Tool class for validation hooks
  • Enhanced Tool.__post_init__() to validate inputs_from_state against valid parameters and outputs_to_state against valid outputs
  • Implemented validation using function introspection + JSON schema union for robustness
  • ComponentTool overrides validation methods to return component input/output socket names
  • Added comprehensive tests covering validation, edge cases, and backward compatibility

How can it be used

# Typo caught at construction time:
tool = ComponentTool(
    component=my_component,
    inputs_from_state={"docs": "documets"}  # ❌ Typo detected immediately
)
# ValueError: inputs_from_state maps 'docs' to unknown parameter 'documets'

# Correct usage:
tool = ComponentTool(
    component=my_component,
    inputs_from_state={"docs": "documents"}  # ✅ Validated
)

How did you test it

  • Added 7 new unit tests for validation logic (base Tool + ComponentTool)
  • Tested function introspection, schema fallback, and union approach
  • Verified ComponentTool socket validation

Notes for the reviewer

Validation is DRY - all logic in base Tool.__post_init__(). Subclasses only override _get_valid_inputs()/_get_valid_outputs() to provide their valid parameter/output sets. Function introspection + schema union ensures robustness for all tool types (function-based, ComponentTool, PipelineTool).

@vercel
Copy link

vercel bot commented Dec 17, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Review Updated (UTC)
haystack-docs Ignored Ignored Preview Jan 2, 2026 9:13am

@vblagoje vblagoje added the ignore-for-release-notes PRs with this flag won't be included in the release notes. label Dec 17, 2025
@github-actions github-actions bot added topic:tests type:documentation Improvements on the docs labels Dec 17, 2025
@coveralls
Copy link
Collaborator

coveralls commented Dec 17, 2025

Pull Request Test Coverage Report for Build 20304787960

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 7 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.002%) to 92.156%

Files with Coverage Reduction New Missed Lines %
tools/component_tool.py 2 94.51%
tools/tool.py 5 95.58%
Totals Coverage Status
Change from base Build 20295900674: 0.002%
Covered Lines: 14157
Relevant Lines: 15362

💛 - Coveralls

@vblagoje vblagoje requested a review from sjrl December 17, 2025 13:37
@vblagoje vblagoje marked this pull request as ready for review December 17, 2025 13:37
@vblagoje vblagoje requested a review from a team as a code owner December 17, 2025 13:37
@vblagoje vblagoje removed the ignore-for-release-notes PRs with this flag won't be included in the release notes. label Dec 17, 2025
@vblagoje vblagoje removed the request for review from a team December 17, 2025 13:40
@vblagoje
Copy link
Member Author

@sjrl this is the best approach I found to support validation of state kys from Tool side including ComponentTool. LMK and we can adjust if needed

@sjrl
Copy link
Contributor

sjrl commented Dec 19, 2025

@vblagoje taking a look now!

I wanted to ask could we also add a test for PipelineTool which inherits from ComponentTool? It would be good to check that also works as expected.

f"Valid outputs are: {valid_outputs}."
)

def _get_valid_inputs(self) -> set[str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this and _get_valid_outputs also need to be implemented for MCPTool?

Or would we need to turn of this validation for MCPTool if eager_connect is False since we wouldn't know what the inputs or outputs of the MCPTool would be at that stage right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, exactly, in MCPTool we would check only when eager is true. Otherwise we simply return None to bypass this validation

@vblagoje
Copy link
Member Author

@vblagoje taking a look now!

I wanted to ask could we also add a test for PipelineTool which inherits from ComponentTool? It would be good to check that also works as expected.

Nice catch will do that as well! And add it to this PR

@vblagoje
Copy link
Member Author

vblagoje commented Jan 2, 2026

Updated to 3.10 and added PipelineTool tests, please see if I missed something @sjrl

Comment on lines +124 to +134
# Validate that outputs_to_state source keys exist as valid tool outputs
if self.outputs_to_state is not None:
valid_outputs: set[str] | None = self._get_valid_outputs()
if valid_outputs is not None:
for state_key, config in self.outputs_to_state.items():
source = config.get("source")
if source is not None and source not in valid_outputs:
raise ValueError(
f"outputs_to_state: '{self.name}' maps state key '{state_key}' to unknown output '{source}'"
f"Valid outputs are: {valid_outputs}."
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this under line 94 since there we are already doing some validation on outputs_to_state. So I think it makes sense to group these two together.

Copy link
Contributor

@sjrl sjrl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vblagoje looks really good! Just a few minor comments

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

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ComponentTool should validate inputs_from_state, outputs_to_state

4 participants