Skip to content

feat: dspy - Enable DSPy with dynamic context (e.g. from RAG)#3145

Open
tomasruizt wants to merge 2 commits intodeepset-ai:mainfrom
tomasruizt:feat/dspy-pipeline-inputs
Open

feat: dspy - Enable DSPy with dynamic context (e.g. from RAG)#3145
tomasruizt wants to merge 2 commits intodeepset-ai:mainfrom
tomasruizt:feat/dspy-pipeline-inputs

Conversation

@tomasruizt
Copy link
Copy Markdown

@tomasruizt tomasruizt commented Apr 13, 2026

Related Issues

Context

The original DSPy + Haystack cookbook notebook demonstrates a RAG flow where a retriever dynamically looks up the context for each question. With the recently merged DSPySignatureChatGenerator (#2831), this exact use case is not yet possible inside a Haystack Pipeline:

  • run() declares only messages and generation_kwargs as input sockets.
  • Signature fields like context live behind **kwargs, so Pipeline.connect() cannot bind anything to them.

The net effect: today the chat generator only works with static, hardcoded context (as in test_run_with_input_mapping and test_live_run_with_multi_field_signature). A retriever cannot dynamically supply question-dependent context.

Proposed Changes

Add a pipeline_inputs: list[str] parameter to DSPySignatureChatGenerator. When set, the listed signature input fields are registered as real Haystack input sockets via component.set_input_type(self, name, str, ""). This unlocks the pipeline input/output discovery mechanism, so a retriever can be wired directly to the generator and the context is resolved at run time, per question. See the new unit test test_rag_pipeline_question_dependent_context for a concrete usage example.

The change should be backwards compatible: when pipeline_inputs is None, the existing behavior is unchanged. Let me know if this PR makes sense in your opinion @anakin87 @julian-risch

How did you test it?

  • Added a unit test test_rag_pipeline_question_dependent_context that wires InMemoryBM25Retriever to the DSPySignatureChatGenerator inside a Pipeline and asserts the retrieved context reaching the (mocked) DSPy module is question-dependent.
  • hatch run test:unit — 39 passed
  • hatch run test:types — no issues
  • hatch run fmt — clean

Checklist

AI disclosure: Claude Code was used.

Add a `pipeline_inputs` parameter to `DSPySignatureChatGenerator` listing
signature input fields that should be exposed as Haystack pipeline input
sockets via `component.set_input_type`. Without this, signature fields are
only reachable through `**kwargs` on direct `run()` calls and are invisible
to `Pipeline.connect()`, which makes RAG-style wiring
(retriever.documents -> llm.context) impossible.

Add a unit test that wires an `InMemoryBM25Retriever` to the chat generator
inside a `Pipeline` and asserts the retrieved context reaching the DSPy
module is question-dependent.

Related to deepset-ai#1635.
@github-actions github-actions Bot added the type:documentation Improvements or additions to documentation label Apr 13, 2026
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions
Copy link
Copy Markdown
Contributor

Coverage report (dspy)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/dspy/src/haystack_integrations/components/generators/dspy/chat
  chat_generator.py
Project Total  

This report was generated by python-coverage-comment-action

@tomasruizt tomasruizt marked this pull request as ready for review April 13, 2026 12:36
@tomasruizt tomasruizt requested a review from a team as a code owner April 13, 2026 12:36
@tomasruizt tomasruizt requested review from davidsbatista and removed request for a team April 13, 2026 12:36
@tomasruizt tomasruizt changed the title feat: dspy - expose pipeline_inputs as Haystack input sockets for RAG feat: dspy - Enable DSPy with dynamic context (e.g. from RAG) Apr 13, 2026
@anakin87 anakin87 requested review from anakin87 and removed request for davidsbatista April 13, 2026 14:08
@anakin87
Copy link
Copy Markdown
Member

I'll take a look in the next few days...

@anakin87
Copy link
Copy Markdown
Member

@julian-risch I'd also appreciate your opinion

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

Labels

integration:dspy type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants