Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

perf(brain): probe openai stream_options lazily, not at import

rubenluetke10-beep9cdd06830ce2
diff --git a/jarvis/plugins/brain/_openai_base.py b/jarvis/plugins/brain/_openai_base.py
index 761d472956..399ec45754 100644
--- a/jarvis/plugins/brain/_openai_base.py
+++ b/jarvis/plugins/brain/_openai_base.py
@@ -7,6 +7,7 @@ All three use the Chat-Completions format. Differences:
"""
from __future__ import annotations
+import functools
import inspect
import json
import logging
@@ -29,30 +30,39 @@ log = logging.getLogger(__name__)
CLIENT_TIMEOUT = httpx.Timeout(connect=5.0, read=30.0, write=30.0, pool=30.0)
def _stream_options_supported() -> bool:
"""One-shot detection: does the installed openai SDK know ``stream_options``?
`stream_options` was introduced in openai>=1.30 (June 2024) — older
versions raise ``TypeError: got unexpected keyword argument`` directly
- on the call. We check the signature once at module-load time and cache
- the result. For future API changes, the re-try path in
+ on the call. We check the signature once, on the first chat call, and
+ cache the result. For future API changes, the re-try path in
``run_openai_chat`` is the safety net.
+
+ Deliberately NOT evaluated at module import: the probe imports the whole
+ openai SDK (~1-2 s of pydantic model construction warm, 5-13 s on a busy
+ cold boot), and this module is imported by the speech pipeline
+ constructor and the realtime transport warm-up, both of which run ON the
+ backend event loop during boot. Every WebSocket and route froze behind it
+ (the "loop thread never identified" 15 s boot stalls in the desktop log).
+ The first chat call needs the SDK anyway, so the cost lands where it is
+ already paid.
"""
try:
from openai.resources.chat.completions import AsyncCompletions
sig = inspect.signature(AsyncCompletions.create)
- return "stream_options" in sig.parameters
- except Exception: # noqa: BLE001 — detection must never kill the import
- return False
-
-
-_STREAM_OPTIONS_SUPPORTED = _stream_options_supported()
-if not _STREAM_OPTIONS_SUPPORTED:
- log.warning(
- "openai SDK does not know 'stream_options' — likely openai<1.30. "
- "Provider runs without inline usage tracking. Recommendation: pip install -U openai."
- )
+ supported = "stream_options" in sig.parameters
+ except Exception: # noqa: BLE001 — detection must never kill a chat call
+ supported = False
+ if not supported:
+ log.warning(
+ "openai SDK does not know 'stream_options' — likely openai<1.30. "
+ "Provider runs without inline usage tracking. "
+ "Recommendation: pip install -U openai."
+ )
+ return supported
def _tool_output_text(message: BrainMessage) -> str:
@@ -546,7 +556,7 @@ async def stream_complete(
# the unconditional call would raise a TypeError and crash the plugin
# chain with "AsyncCompletions.create() got an unexpected keyword argument"
# — the user then hears the "unreachable" diagnostic instead of an answer.
- if _STREAM_OPTIONS_SUPPORTED:
+ if _stream_options_supported():
kwargs["stream_options"] = {"include_usage": True}
# Sanitize tool names to the OpenAI/Anthropic rule and keep a reverse map so
# the model's tool_call resolves back to the ORIGINAL tool name (e.g. the