deepset-ai / haystack

:mag: AI orchestration framework to build customizable, production-ready LLM applications. Connect components (models, vector DBs, file converters) to pipelines or agents that can interact with your data. With advanced retrieval methods, it's best suited for building RAG, question answering, semantic search or conversational agent chatbots.
https://haystack.deepset.ai
Apache License 2.0
16.94k stars 1.85k forks source link

`component.set_input_types()` and `component.set_input_type()` let's user set non-existing inputs #8280

Closed silvanocerza closed 2 weeks ago

silvanocerza commented 1 month ago

Describe the bug

If a user calls component.set_input_types() or component.set_input_type() in a Component's init method the inputs will be added even if the Component doesn't define a run method that accepts **kwargs.

Expected behavior

Setting an input in a Component init that has a run that doesn't accept **kwargs must fail.

To Reproduce

This sould fail:

@component
class Foo:
    def __init__(self):
        component.set_input_types(self, y=int)

    @component.output_types(output=int)
    def run(self, x: int):
        return {"output": x}

print(Foo())
# <__main__.Foo object at 0x106190500>
# Inputs:
#   - y: int
#   - x: int
# Outputs:
#   - output: int

This should fail too:

@component
class Bar:
    def __init__(self):
        component.set_input_type(self, "y", int)

    @component.output_types(output=int)
    def run(self, x: int):
        return {"output": x}

print(Bar())
# <__main__.Bar object at 0x13998ea80>
# Inputs:
#   - y: int
#   - x: int
# Outputs:
#   - output: int

This is correct instead:

@component
class Baz:
    def __init__(self):
        component.set_input_type(self, "y", int)

    @component.output_types(output=int)
    def run(self, x: int, **kwargs):
        return {"output": x + kwargs.get("y")}

print(Baz())
# <__main__.Baz object at 0x13998ec30>
# Inputs:
#   - y: int
#   - x: int
# Outputs:
#   - output: int
shadeMe commented 2 weeks ago

We should also raise an error if set_input_types overrides the type of an existing non-kwarg parameter of the run method.