{"record":{"id":"21f2b10c3061f0f3","repo":"microsoft/autogen","slug":"failed-to-create-openai-embedding-function-with-mo","errorCode":null,"errorMessage":"Failed to create OpenAI embedding function with model '{config.model_name}'. Ensure openai is installed and API key is valid. Error: {e}","messagePattern":"Failed to create OpenAI embedding function with model '(.+?)'\\. Ensure openai is installed and API key is valid\\. Error: (.+?)","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"critical","filePath":"python/packages/autogen-ext/src/autogen_ext/memory/chromadb/_chromadb.py","lineNumber":224,"sourceCode":"        config = self._config.embedding_function_config\n\n        if isinstance(config, DefaultEmbeddingFunctionConfig):\n            return embedding_functions.DefaultEmbeddingFunction()\n\n        elif isinstance(config, SentenceTransformerEmbeddingFunctionConfig):\n            try:\n                return embedding_functions.SentenceTransformerEmbeddingFunction(model_name=config.model_name)\n            except Exception as e:\n                raise ImportError(\n                    f\"Failed to create SentenceTransformer embedding function with model '{config.model_name}'. \"\n                    f\"Ensure sentence-transformers is installed and the model is available. Error: {e}\"\n                ) from e\n\n        elif isinstance(config, OpenAIEmbeddingFunctionConfig):\n            try:\n                return embedding_functions.OpenAIEmbeddingFunction(api_key=config.api_key, model_name=config.model_name)\n            except Exception as e:\n                raise ImportError(\n                    f\"Failed to create OpenAI embedding function with model '{config.model_name}'. \"\n                    f\"Ensure openai is installed and API key is valid. Error: {e}\"\n                ) from e\n\n        elif isinstance(config, CustomEmbeddingFunctionConfig):\n            try:\n                return config.function(**config.params)\n            except Exception as e:\n                raise ValueError(f\"Failed to create custom embedding function. Error: {e}\") from e\n\n        else:\n            raise ValueError(f\"Unsupported embedding function config type: {type(config)}\")\n\n    def _ensure_initialized(self) -> None:\n        \"\"\"Ensure ChromaDB client and collection are initialized.\"\"\"\n        if self._client is None:\n            try:\n                from chromadb.config import Settings","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/memory/chromadb/_chromadb.py#L206-L242","documentation":"With OpenAIEmbeddingFunctionConfig, constructing chromadb's OpenAIEmbeddingFunction is wrapped in try/except; failures such as an invalid API key format, missing openai package, or bad model name are re-raised as ImportError with the model name and underlying error.","triggerScenarios":"Configuring ChromaDBVectorMemory with OpenAIEmbeddingFunctionConfig(api_key=..., model_name=...) and initializing it when the openai package is absent, the API key is None/empty/invalid, or the model name is not an embedding model (e.g. a chat model id).","commonSituations":"Forgetting to pass the API key (env var not read); using a chat model name like 'gpt-4o' instead of 'text-embedding-3-small'; openai not installed in the deployment image.","solutions":["Pass a valid API key explicitly or load it from the environment before building the config.","Use an embedding model id, e.g. 'text-embedding-3-small' or 'text-embedding-3-large'.","Install the openai package: pip install openai.","Inspect the trailing 'Error: {e}' for the precise failure (auth vs model vs import)."],"exampleFix":"# before\nconfig = OpenAIEmbeddingFunctionConfig(api_key=None, model_name=\"gpt-4o\")\n# after\nconfig = OpenAIEmbeddingFunctionConfig(api_key=os.environ[\"OPENAI_API_KEY\"], model_name=\"text-embedding-3-small\")","handlingStrategy":"validation","validationCode":"import os\napi_key = os.environ.get(\"OPENAI_API_KEY\")\nassert api_key, \"OPENAI_API_KEY is required for OpenAIEmbeddingFunctionConfig\"\nassert model_name.startswith(\"text-embedding\"), f\"{model_name} is not an embedding model\"\nconfig = OpenAIEmbeddingFunctionConfig(api_key=api_key, model_name=model_name)","typeGuard":"def is_openai_embedding_model(name: str) -> bool:\n    return name.startswith(\"text-embedding-\")","tryCatchPattern":"try:\n    memory = ChromaDBVectorMemory(config=config)\nexcept ImportError as e:\n    if \"OpenAI embedding function\" in str(e):\n        # invalid key, missing openai pkg, or wrong model id — check e.__cause__\n        raise\n    raise","preventionTips":["Always pass an explicit, validated API key from the environment.","Use embedding model ids (text-embedding-3-small/large), never chat model ids.","Install the openai package alongside chromadb."],"tags":["autogen","chromadb","embeddings","openai","authentication"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}