Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion dspy/streaming/streaming_listener.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import re
from collections import defaultdict
from queue import Queue
Expand Down Expand Up @@ -135,7 +136,12 @@ def receive(self, chunk: ModelResponseStream):
return

# Handle custom streamable types
if self._output_type and issubclass(self._output_type, Type) and self._output_type.is_streamable():
if (
self._output_type
and inspect.isclass(self._output_type)
and issubclass(self._output_type, Type)
and self._output_type.is_streamable()
):
if parsed_chunk := self._output_type.parse_stream_chunk(chunk):
return StreamResponse(
self.predict_name,
Expand Down
46 changes: 46 additions & 0 deletions tests/streaming/test_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,52 @@ async def chat_stream(*args, **kwargs):
assert "success" in full_content


@pytest.mark.anyio
async def test_chat_adapter_with_generic_type_annotation():
class TestSignature(dspy.Signature):
question: str = dspy.InputField()
response: list[str] | int = dspy.OutputField()

class MyProgram(dspy.Module):
def __init__(self):
self.predict = dspy.Predict(TestSignature)

def forward(self, question, **kwargs):
return self.predict(question=question, **kwargs)

async def chat_stream(*args, **kwargs):
yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="[[ ##"))])
yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content=" response"))])
yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content=" ## ]]\n\n"))])
yield ModelResponseStream(
model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="1"))]
)
yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="\n\n[[ ##"))])
yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content=" completed"))])
yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content=" ## ]]"))])

program = dspy.streamify(
MyProgram(),
stream_listeners=[
dspy.streaming.StreamListener(signature_field_name="response"),
],
)

with mock.patch("litellm.acompletion", side_effect=chat_stream):
with dspy.context(lm=dspy.LM("openai/gpt-4o-mini", cache=False), adapter=dspy.ChatAdapter()):
output = program(question="Say hello")
chunks = []
async for value in output:
if isinstance(value, StreamResponse):
chunks.append(value)

assert len(chunks) > 0
assert chunks[0].signature_field_name == "response"

full_content = "".join(chunk.chunk for chunk in chunks)
assert "1" in full_content


@pytest.mark.anyio
async def test_chat_adapter_nested_pydantic_streaming():
"""Test ChatAdapter streaming with nested pydantic model."""
Expand Down