Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions dspy/streaming/streaming_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ def flush(self) -> str:
boundary_index = len(last_tokens)
return last_tokens[:boundary_index]
elif isinstance(settings.adapter, ChatAdapter) or settings.adapter is None:
boundary_index = last_tokens.find("[[")
if boundary_index == -1:
boundary_index = len(last_tokens)
end_identifier = self.adapter_identifiers["ChatAdapter"]["end_identifier"]
boundary = end_identifier.search(last_tokens)
boundary_index = boundary.start() if boundary else len(last_tokens)
return last_tokens[:boundary_index]
else:
raise ValueError(
Expand Down
22 changes: 22 additions & 0 deletions tests/streaming/test_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,28 @@ async def multi_field_stream(*args, **kwargs):
assert "second response" in second_content


@pytest.mark.parametrize("adapter", [None, dspy.ChatAdapter()])
@pytest.mark.parametrize(
"content_chunks",
[
["Matrix: [[1, 2], [3, 4]]", "\n\n[[ ## completed ## ]]"],
["Matrix: [", "[", "1, 2], [3, 4]]", "\n\n[[ ## completed ## ]]"],
["Matrix: [[1, 2], [3, 4]]\n\n[[ ## completed ## ]]"],
],
)
def test_stream_listener_preserves_literal_double_brackets(adapter, content_chunks):
listener = dspy.streaming.StreamListener(signature_field_name="answer")
chunks = ["[[ ## answer", " ## ]]\n\n", *content_chunks]
outputs = []
with dspy.context(adapter=adapter):
for text in chunks:
chunk = ModelResponseStream(choices=[StreamingChoices(delta=Delta(content=text))])
if output := listener.receive(chunk):
outputs.append(output)
assert "".join(output.chunk for output in outputs) == "Matrix: [[1, 2], [3, 4]]"
assert outputs[-1].is_last_chunk


def test_stream_listener_could_form_end_identifier_chat_adapter():
listener = dspy.streaming.StreamListener(signature_field_name="answer")

Expand Down
Loading