Skip to content

Commit 49ddc4c

Browse files
committed
fix(docs): make ai_shell example sphinx-gallery compatible
- Rename to ai_shell_machine.py to match gallery filename_pattern - Move PEP 723 metadata after docstring so gallery finds the RST title - Lazy-import openai so the class definition works without the package (allows diagram generation during docs build) - Skip sphinx-gallery execution via sys.modules guard - Graceful degradation: main() prints a message if openai is missing
1 parent b4265c6 commit 49ddc4c

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import sys
4040
import threading
4141

42-
from openai import OpenAI # type: ignore[import-not-found]
43-
4442
from statemachine import HistoryState
4543
from statemachine import State
4644
from statemachine import StateChart
@@ -410,6 +408,8 @@ class context_tracker(State.Compound):
410408
# --- Initialization ---
411409

412410
def __init__(self):
411+
from openai import OpenAI # type: ignore[import-not-found]
412+
413413
self.client = OpenAI()
414414
self.messages: list = [{"role": "system", "content": SYSTEM_PROMPT}]
415415
self._last_text: str = ""
@@ -510,7 +510,22 @@ def on_enter_conversation_ended(self, **kwargs):
510510
# ---------------------------------------------------------------------------
511511

512512

513+
def _check_openai():
514+
"""Return True if the openai package is available."""
515+
try:
516+
import openai # noqa: F401
517+
518+
return True
519+
except ImportError:
520+
return False
521+
522+
513523
def main():
524+
if not _check_openai():
525+
print("This example requires the 'openai' package.")
526+
print("Install it with: pip install openai")
527+
return
528+
514529
print("AI Shell")
515530
print("A coding assistant powered by python-statemachine + OpenAI.")
516531
print("Type 'bye', 'exit', or 'quit' to end. Ctrl+C to interrupt.")
@@ -534,5 +549,5 @@ def main():
534549
sm.send("user_message", text=text)
535550

536551

537-
if __name__ == "__main__":
552+
if __name__ == "__main__" and "sphinx" not in sys.modules: # pragma: no cover
538553
main()

0 commit comments

Comments
 (0)