{"record":{"id":"8264fca8a7053040","repo":"huggingface/smolagents","slug":"please-install-openai-extra-to-use-openaimodel","errorCode":null,"errorMessage":"Please install 'openai' extra to use OpenAIModel: `pip install 'smolagents[openai]'`","messagePattern":"Please install 'openai' extra to use OpenAIModel: `pip install 'smolagents\\[openai\\]'`","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"error","filePath":"src/smolagents/models.py","lineNumber":1701,"sourceCode":"        self.client_kwargs = {\n            **(client_kwargs or {}),\n            \"api_key\": api_key,\n            \"base_url\": api_base,\n            \"organization\": organization,\n            \"project\": project,\n        }\n        super().__init__(\n            model_id=model_id,\n            custom_role_conversions=custom_role_conversions,\n            flatten_messages_as_text=flatten_messages_as_text,\n            **kwargs,\n        )\n\n    def create_client(self):\n        try:\n            import openai\n        except ModuleNotFoundError as e:\n            raise ModuleNotFoundError(\n                \"Please install 'openai' extra to use OpenAIModel: `pip install 'smolagents[openai]'`\"\n            ) from e\n\n        return openai.OpenAI(**self.client_kwargs)\n\n    def generate_stream(\n        self,\n        messages: list[ChatMessage | dict],\n        stop_sequences: list[str] | None = None,\n        response_format: dict[str, str] | None = None,\n        tools_to_call_from: list[Tool] | None = None,\n        **kwargs,\n    ) -> Generator[ChatMessageStreamDelta]:\n        completion_kwargs = self._prepare_completion_kwargs(\n            messages=messages,\n            stop_sequences=stop_sequences,\n            response_format=response_format,\n            tools_to_call_from=tools_to_call_from,","sourceCodeStart":1683,"sourceCodeEnd":1719,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/models.py#L1683-L1719","documentation":"OpenAIModel.create_client imports the openai package; if it is missing, the ModuleNotFoundError is re-raised with instructions to install the `openai` extra. The class definition itself doesn't require openai, but constructing the model does.","triggerScenarios":"Instantiating OpenAIModel (create_client is called from __init__) in an environment where the openai package is not installed — i.e. `pip install smolagents` without extras.","commonSituations":"Base smolagents install used with a custom OpenAI-compatible endpoint (vLLM, Ollama, LM Studio); slim Docker/CI images; stale lockfiles; wrong virtualenv activated.","solutions":["Install the extra: `pip install 'smolagents[openai]'` (equivalent to adding openai).","Confirm with `python -c \"import openai\"` in the interpreter you actually run.","Add the extra to requirements/pyproject so environments are reproducible."],"exampleFix":"# before\npip install smolagents\nmodel = OpenAIModel(model_id=\"qwen2.5\", api_base=\"http://localhost:11434/v1\")  # ModuleNotFoundError\n\n# after\npip install 'smolagents[openai]'","handlingStrategy":"validation","validationCode":"try:\n    import openai  # noqa\n    ok = True\nexcept ModuleNotFoundError:\n    ok = False\nassert ok, \"pip install 'smolagents[openai]'\"","typeGuard":"null","tryCatchPattern":"try:\n    model = OpenAIModel(model_id=mid, api_base=url)\nexcept ModuleNotFoundError as e:\n    print(\"Install the extra:\", e)\n    sys.exit(1)","preventionTips":["Put smolagents[openai] in requirements for any OpenAI-compatible usage.","Add a startup dependency check for optional SDKs."],"tags":["openai","missing-dependency","pip-extra","installation"],"backgroundTag":"missing-optional-dependency","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}