asyncify python sdk v2#819
Open
prathikr wants to merge 1 commit into
Open
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR converts the Python SDK v2 from synchronous to async, wrapping all blocking native FFI calls with asyncio.to_thread() so they don't block the event loop. The public API surfaces change from sync methods to async def / async for, and tests are updated to use pytest-asyncio with asyncio_mode = "auto".
Changes:
- All SDK public methods that call into the native layer (
load,unload,download,close,shutdown,start_web_service,stop_web_service,discover_eps,complete,stream,transcribe,transcribe_streaming,generate_embedding(s)) are nowasyncand delegate blocking work toasyncio.to_thread. - Chat and audio streaming APIs are renamed (
complete_streaming_chat→stream, synchronous generators → async generators) and the implementation now buffers all streamed items before yielding. - Test infrastructure is updated to pytest-asyncio with a session-scoped event loop, and all integration tests are converted to
async def.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
sdk_v2/python/src/foundry_local_sdk/foundry_local_manager.py |
Converts singleton manager to async: asyncio.Lock, async initialize/close/shutdown/web-service/EP methods, __aenter__/__aexit__, atexit handler, __del__ |
sdk_v2/python/src/foundry_local_sdk/imodel.py |
Makes download, load, unload async with asyncio.to_thread |
sdk_v2/python/src/foundry_local_sdk/openai/chat_client.py |
Renames complete_chat → complete, complete_streaming_chat → stream; async via asyncio.to_thread |
sdk_v2/python/src/foundry_local_sdk/openai/embedding_client.py |
Makes generate_embedding(s) async via asyncio.to_thread |
sdk_v2/python/src/foundry_local_sdk/openai/audio_client.py |
Makes transcribe / transcribe_streaming async via asyncio.to_thread |
sdk_v2/python/src/foundry_local_sdk/request.py |
Adds cancel_async thin wrapper |
sdk_v2/python/test/conftest.py |
Session-scoped async fixtures, deprecated event_loop fixture override |
sdk_v2/python/pyproject.toml |
Adds pytest-asyncio dependency and asyncio_mode = "auto" |
sdk_v2/python/test/integration/test_chat_client.py |
Converts chat tests to async |
sdk_v2/python/test/integration/test_embedding_client.py |
Converts embedding tests to async |
sdk_v2/python/test/integration/test_audio_client.py |
Converts audio tests to async |
sdk_v2/python/test/integration/test_model_lifecycle.py |
Converts model lifecycle tests to async |
sdk_v2/python/test/integration/test_ep_lifecycle.py |
Converts EP discovery test to async |
sdk_v2/python/test/integration/test_web_service_and_eps.py |
Converts web service/EP tests to async |
sdk_v2/python/test/integration/test_zz_manager_shutdown.py |
Converts shutdown tests to async |
sdk_v2/python/test/integration/test_zz_singleton_recreate.py |
Converts singleton recreate tests to async |
Comment on lines
+264
to
+277
| def _blocking_stream(): | ||
| """Run blocking streaming in a separate thread.""" | ||
| items = [] | ||
| with ChatSession(self._model) as session: | ||
| session.set_streaming(True) | ||
| with Request() as request: | ||
| request.add_item(TextItem(request_json, TextItemType.OPENAI_JSON)) | ||
| with session.process_streaming_request(request) as stream: | ||
| for item in stream: | ||
| items.append(item) | ||
| return items | ||
|
|
||
| # Run blocking operation in thread and yield each result | ||
| items = await asyncio.to_thread(_blocking_stream) |
Comment on lines
+176
to
+189
| def _blocking_stream(): | ||
| """Run blocking streaming in a separate thread.""" | ||
| items = [] | ||
| with AudioSession(self._model) as session: | ||
| session.set_streaming(True) | ||
| with Request() as request: | ||
| request.add_item(TextItem(request_json, TextItemType.OPENAI_JSON)) | ||
| with session.process_streaming_request(request) as stream: | ||
| for item in stream: | ||
| items.append(item) | ||
| return items | ||
|
|
||
| # Run blocking operation in thread and yield each result | ||
| items = await asyncio.to_thread(_blocking_stream) |
Comment on lines
356
to
361
| def __del__(self) -> None: | ||
| # Best-effort safety net — production code should call close() explicitly. | ||
| try: | ||
| self.close() | ||
| if self._native_manager is not None: | ||
| asyncio.run(self.close()) | ||
| except Exception: |
Comment on lines
316
to
+348
| @@ -314,15 +345,18 @@ def close(self) -> None: | |||
| if FoundryLocalManager.instance is self: | |||
| FoundryLocalManager.instance = None | |||
|
|
|||
| def __enter__(self) -> "FoundryLocalManager": | |||
| return await asyncio.to_thread(_close) | |||
| import threading | ||
| from typing import Callable | ||
| import asyncio | ||
| from typing import AsyncGenerator, Callable |
| import asyncio | ||
| from abc import ABC, abstractmethod | ||
| from typing import TYPE_CHECKING, Callable | ||
| from typing import TYPE_CHECKING, AsyncGenerator, Callable |
Comment on lines
+52
to
+56
| @pytest.fixture(scope="session") | ||
| def event_loop(): | ||
| loop = asyncio.new_event_loop() | ||
| yield loop | ||
| loop.close() |
Comment on lines
+277
to
+292
| async def stop_web_service(self) -> None: | ||
| """Stop the optional built-in web service. | ||
|
|
||
| Raises: | ||
| FoundryLocalException: If the web service is not currently running. | ||
| """ | ||
| from foundry_local_sdk._native.api import api | ||
| if self.urls is None: | ||
| raise FoundryLocalException("Web service is not running.") | ||
|
|
||
| with FoundryLocalManager._lock: | ||
| if self.urls is None: | ||
| raise FoundryLocalException("Web service is not running.") | ||
| def _stop(): | ||
| from foundry_local_sdk._native.api import api | ||
|
|
||
| api.check_status(api.root.Manager_WebServiceStop(self._native_manager)) | ||
| self.urls = None | ||
|
|
||
| return await asyncio.to_thread(_stop) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.