Skip to content

Conversation

@TomeHirata
Copy link
Collaborator

Currently, the following code does not work using streaming and dspy.context since the context vars are not kept in the generator. This PR fixes this issue by keeping the context vars when the generator is created during iteration.

import dspy
import asyncio

class MyDSPymodule(dspy.Module):
  def __init__(self):
    self.predict = dspy.Predict("question->answer")
    self.llm = dspy.LM("openai/gpt-4.1", cache=False)

  def forward(self, question: str):
    with dspy.context(lm=self.llm):
        stream_predict = dspy.streamify(
            self.predict,
            stream_listeners=[dspy.streaming.StreamListener(signature_field_name="answer")],
        )
        return stream_predict(question=question)

async def read_output_stream():
    gen = MyDSPymodule()
    output_stream = gen(question="Why did a chicken cross the kitchen?")
    async for chunk in output_stream: # <- the actual request happen here and lm
        print(chunk)

asyncio.run(read_output_stream())

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a context propagation issue in DSPy's streaming functionality where context variables (like LM settings) were lost after exiting a dspy.context() block but before async iteration began. The fix captures parent context overrides when the streamer is created and restores them in the generator task.

Key changes:

  • Modified streamify to capture and propagate thread-local context overrides across async task boundaries
  • Added test coverage to verify context variables persist correctly through the streaming lifecycle

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
dspy/streaming/streamify.py Modified async_streamer and generator functions to capture and restore parent context overrides
tests/streaming/test_streaming.py Added test case and imports to verify context propagation through streamify

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Collaborator

@chenmoneygithub chenmoneygithub left a comment

Choose a reason for hiding this comment

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

Discussed offline - it's not 100% safe to carry the contextvar out of its scope.

Instead we can raise an explicit warning here:

async for value in receive_stream:
, to notify users to configure dspy.LM when coonsuming the streaming generator.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants