@@ -229,6 +229,36 @@ def __call__(self, x: str, **kwargs):
229
229
assert all_chunks [- 1 ].signature_field_name == "judgement"
230
230
231
231
232
+ @pytest .mark .anyio
233
+ async def test_streaming_handles_space_correctly ():
234
+ dspy .settings .configure (lm = dspy .LM ("openai/gpt-4o-mini" , cache = False ), adapter = dspy .ChatAdapter ())
235
+ my_program = dspy .Predict ("question->answer" )
236
+ program = dspy .streamify (
237
+ my_program , stream_listeners = [dspy .streaming .StreamListener (signature_field_name = "answer" )]
238
+ )
239
+
240
+ async def gpt_4o_mini_stream (* args , ** kwargs ):
241
+ yield ModelResponseStream (
242
+ model = "gpt-4o-mini" , choices = [StreamingChoices (delta = Delta (content = "[[ ## answer ## ]]\n " ))]
243
+ )
244
+ yield ModelResponseStream (model = "gpt-4o-mini" , choices = [StreamingChoices (delta = Delta (content = "How " ))])
245
+ yield ModelResponseStream (model = "gpt-4o-mini" , choices = [StreamingChoices (delta = Delta (content = "are " ))])
246
+ yield ModelResponseStream (model = "gpt-4o-mini" , choices = [StreamingChoices (delta = Delta (content = "you " ))])
247
+ yield ModelResponseStream (model = "gpt-4o-mini" , choices = [StreamingChoices (delta = Delta (content = "doing?" ))])
248
+ yield ModelResponseStream (
249
+ model = "gpt-4o-mini" , choices = [StreamingChoices (delta = Delta (content = "\n \n [[ ## completed ## ]]" ))]
250
+ )
251
+
252
+ with mock .patch ("litellm.acompletion" , side_effect = gpt_4o_mini_stream ):
253
+ output = program (question = "What is the capital of France?" )
254
+ all_chunks = []
255
+ async for value in output :
256
+ if isinstance (value , dspy .streaming .StreamResponse ):
257
+ all_chunks .append (value )
258
+
259
+ assert all_chunks [0 ].chunk == "How are you doing?"
260
+
261
+
232
262
@pytest .mark .skipif (not os .getenv ("OPENAI_API_KEY" ), reason = "OpenAI API key not found in environment variables" )
233
263
def test_sync_streaming ():
234
264
class MyProgram (dspy .Module ):
0 commit comments