From 302d72cc47553d65bab92cf0bf7b276eb8ec0ac6 Mon Sep 17 00:00:00 2001 From: Neil Mehta Date: Tue, 18 Mar 2025 15:53:41 -0400 Subject: [PATCH] Fix python3.10 async --- .../remote/inference/lmstudio/_client.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/llama_stack/providers/remote/inference/lmstudio/_client.py b/llama_stack/providers/remote/inference/lmstudio/_client.py index ad3341ae7..5359585b0 100644 --- a/llama_stack/providers/remote/inference/lmstudio/_client.py +++ b/llama_stack/providers/remote/inference/lmstudio/_client.py @@ -94,7 +94,6 @@ class LMStudioClient: chat = self._convert_message_list_to_lmstudio_chat(messages) config = self._get_completion_config_from_params(sampling_params) if stream: - async def stream_generator(): prediction_stream = await asyncio.to_thread( llm.respond_stream, @@ -209,7 +208,6 @@ class LMStudioClient: ) -> Union[CompletionResponse, AsyncIterator[CompletionResponseStreamChunk]]: config = self._get_completion_config_from_params(sampling_params) if stream: - async def stream_generator(): prediction_stream = await asyncio.to_thread( llm.complete_stream, @@ -308,11 +306,18 @@ class LMStudioClient: async def _async_iterate(self, iterable): iterator = iter(iterable) - while True: + + def safe_next(it): try: - yield await asyncio.to_thread(next, iterator) - except: + return (next(it), False) + except StopIteration: + return (None, True) + + while True: + item, done = await asyncio.to_thread(safe_next, iterator) + if done: break + yield item async def _convert_request_to_rest_call( self, request: ChatCompletionRequest