jiter_preload.py 915 B

1234567891011121314151617181920212223242526272829
  1. """Best-effort early import of the OpenAI SDK's native streaming parser: on some
  2. Windows installs ``jiter``'s native extension imports fine from the venv but fails
  3. when first imported inside the threaded streaming path. Loading it once at
  4. agent-package import avoids that while keeping the SDK's normal error path for
  5. genuinely broken installs."""
  6. from __future__ import annotations
  7. import importlib
  8. _JITER_PRELOADED = False
  9. _JITER_PRELOAD_ERROR: Exception | None = None
  10. def preload_jiter_native_extension() -> bool:
  11. global _JITER_PRELOADED, _JITER_PRELOAD_ERROR
  12. if _JITER_PRELOADED:
  13. return True
  14. try:
  15. importlib.import_module("jiter.jiter")
  16. from jiter import from_json as _from_json
  17. except Exception as exc:
  18. _JITER_PRELOAD_ERROR = exc
  19. return False
  20. _JITER_PRELOADED, _JITER_PRELOAD_ERROR = True, None
  21. return True
  22. preload_jiter_native_extension()