Skip to content

Commit 9ea3285

Browse files
fix: only warn user when sync api being called without JSPI
1 parent 2116dc0 commit 9ea3285

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

src/promplate_pyodide/__init__.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,9 @@ def wrapper(*args, **kwargs):
7777
if isfunction(func) and func.__name__ == "__call__":
7878
setattr(cls, name, patch_function(func))
7979

80-
if stack_switching_supported():
81-
from .utils.openai.sync import patch_sync_apis
80+
from .utils.openai.sync import patch_sync_apis
8281

83-
patch_sync_apis()
84-
85-
else:
86-
from .utils.warn import NotImplementedWarning
87-
88-
o.TextComplete = o.TextGenerate = o.ChatComplete = o.ChatGenerate = o.SyncTextOpenAI = o.SyncChatOpenAI = o.v1.TextComplete = o.v1.TextGenerate = o.v1.ChatComplete = o.v1.ChatGenerate = o.v1.SyncTextOpenAI = o.v1.SyncChatOpenAI = NotImplementedWarning # fmt: off
82+
patch_sync_apis()
8983

9084

9185
async def patch_openai(fallback_import_url: str = "https://esm.sh/openai"):

src/promplate_pyodide/utils/sync.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from inspect import isawaitable
22
from typing import AsyncIterable, Awaitable, Callable, Iterable, overload
33

4+
from .stack_switching import stack_switching_supported
5+
from .warn import sync_api_warning
6+
47
STOP_ITERATION = object()
58

69

@@ -29,6 +32,8 @@ def to_sync(func): # type: ignore
2932

3033
@wraps(func)
3134
def wrapper(*args, **kwargs):
35+
if not stack_switching_supported():
36+
return sync_api_warning
3237
res = func(*args, **kwargs)
3338
return run_sync(res) if isawaitable(res) else syncify(res)
3439

src/promplate_pyodide/utils/warn.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ def __getitem__(self, _):
1818
return self
1919

2020

21-
NotImplementedWarning = WarnPrinter("This feature does not support pyodide runtime!")
21+
sync_api_warning = WarnPrinter("Synchronous APIs are not available in pyodide unless stack switching is supported.")

0 commit comments

Comments
 (0)