File tree Expand file tree Collapse file tree
src/pydantic_ai_lightspeed/llamastack Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717)
1818from llama_stack .core .server .routes import find_matching_route
1919from llama_stack .core .utils .context import preserve_contexts_async_generator
20+ from starlette .responses import StreamingResponse
2021
2122
2223class _AsyncByteStream (httpx .AsyncByteStream ):
@@ -183,9 +184,16 @@ async def _handle_streaming(
183184 result = await func (** merged_body )
184185
185186 async def gen () -> AsyncGenerator [bytes , None ]:
186- async for chunk in result :
187- data = json .dumps (convert_pydantic_to_json_value (chunk ))
188- yield f"data: { data } \n \n " .encode ("utf-8" )
187+ if isinstance (result , StreamingResponse ):
188+ async for chunk in result .body_iterator :
189+ if isinstance (chunk , str ):
190+ yield chunk .encode ("utf-8" )
191+ else :
192+ yield bytes (chunk )
193+ else :
194+ async for chunk in result :
195+ data = json .dumps (convert_pydantic_to_json_value (chunk ))
196+ yield f"data: { data } \n \n " .encode ("utf-8" )
189197
190198 wrapped_gen = preserve_contexts_async_generator (gen (), [PROVIDER_DATA_VAR ])
191199
You can’t perform that action at this time.
0 commit comments