DSPy: The framework for programming—not prompting—language models
Python
fix style errors of dspy/streaming (#8187)
TomuHirata637d759cfdc4
diff --git a/dspy/streaming/streamify.py b/dspy/streaming/streamify.pyindex 1dfce36f0..98adeacf4 100644--- a/dspy/streaming/streamify.py+++ b/dspy/streaming/streamify.py@@ -267,7 +267,7 @@ async def streaming_response(streamer: AsyncGenerator) -> AsyncGenerator:"""async for value in streamer:if isinstance(value, Prediction):- data = {"prediction": {k: v for k, v in value.items(include_dspy=False)}}+ data = {"prediction": dict(value.items(include_dspy=False))}yield f"data: {ujson.dumps(data)}\n\n"elif isinstance(value, litellm.ModelResponseStream):data = {"chunk": value.json()}diff --git a/dspy/streaming/streaming_listener.py b/dspy/streaming/streaming_listener.pyindex 67370dd8c..c95086b03 100644--- a/dspy/streaming/streaming_listener.py+++ b/dspy/streaming/streaming_listener.py@@ -1,7 +1,7 @@import refrom collections import defaultdictfrom queue import Queue-from typing import TYPE_CHECKING, Any, List+from typing import TYPE_CHECKING, Any, List, Optionalfrom litellm import ModelResponseStream@@ -17,7 +17,7 @@ if TYPE_CHECKING:class StreamListener:"""Class that listens to the stream to capture the streeaming of a specific output field of a predictor."""- def __init__(self, signature_field_name: str, predict: Any = None, predict_name: str = None):+ def __init__(self, signature_field_name: str, predict: Any = None, predict_name: Optional[str] = None):"""Args:signature_field_name: The name of the field to listen to.@@ -36,7 +36,7 @@ class StreamListener:self.stream_end = Falseself.cache_hit = False- self.json_adapter_start_identifier = f'{{"{self.signature_field_name}":"' # noqa: Q000+ self.json_adapter_start_identifier = f'{{"{self.signature_field_name}":"'self.json_adapter_end_identifier = re.compile(r"\w*\",\w*")self.chat_adapter_start_identifier = f"[[ ## {self.signature_field_name} ## ]]"@@ -130,7 +130,7 @@ class StreamListener:last_tokens = "".join(self.field_end_queue.queue)self.field_end_queue = Queue()if isinstance(settings.adapter, JSONAdapter):- boundary_index = last_tokens.find('",') # noqa: Q000+ boundary_index = last_tokens.find('",')return last_tokens[:boundary_index]elif isinstance(settings.adapter, ChatAdapter) or settings.adapter is None:boundary_index = last_tokens.find("[[")