microsoft/autogen · critical · ImportError

Failed to create OpenAI embedding function with model '{conf

Error message

Failed to create OpenAI embedding function with model '{config.model_name}'. Ensure openai is installed and API key is valid. Error: {e}

What it means

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.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/memory/chromadb/_chromadb.py:224

        config = self._config.embedding_function_config

        if isinstance(config, DefaultEmbeddingFunctionConfig):
            return embedding_functions.DefaultEmbeddingFunction()

        elif isinstance(config, SentenceTransformerEmbeddingFunctionConfig):
            try:
                return embedding_functions.SentenceTransformerEmbeddingFunction(model_name=config.model_name)
            except Exception as e:
                raise ImportError(
                    f"Failed to create SentenceTransformer embedding function with model '{config.model_name}'. "
                    f"Ensure sentence-transformers is installed and the model is available. Error: {e}"
                ) from e

        elif isinstance(config, OpenAIEmbeddingFunctionConfig):
            try:
                return embedding_functions.OpenAIEmbeddingFunction(api_key=config.api_key, model_name=config.model_name)
            except Exception as e:
                raise ImportError(
                    f"Failed to create OpenAI embedding function with model '{config.model_name}'. "
                    f"Ensure openai is installed and API key is valid. Error: {e}"
                ) from e

        elif isinstance(config, CustomEmbeddingFunctionConfig):
            try:
                return config.function(**config.params)
            except Exception as e:
                raise ValueError(f"Failed to create custom embedding function. Error: {e}") from e

        else:
            raise ValueError(f"Unsupported embedding function config type: {type(config)}")

    def _ensure_initialized(self) -> None:
        """Ensure ChromaDB client and collection are initialized."""
        if self._client is None:
            try:
                from chromadb.config import Settings

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Pass a valid API key explicitly or load it from the environment before building the config.
  2. Use an embedding model id, e.g. 'text-embedding-3-small' or 'text-embedding-3-large'.
  3. Install the openai package: pip install openai.
  4. Inspect the trailing 'Error: {e}' for the precise failure (auth vs model vs import).

Example fix

# before
config = OpenAIEmbeddingFunctionConfig(api_key=None, model_name="gpt-4o")
# after
config = OpenAIEmbeddingFunctionConfig(api_key=os.environ["OPENAI_API_KEY"], model_name="text-embedding-3-small")
Defensive patterns

Strategy: validation

Validate before calling

import os
api_key = os.environ.get("OPENAI_API_KEY")
assert api_key, "OPENAI_API_KEY is required for OpenAIEmbeddingFunctionConfig"
assert model_name.startswith("text-embedding"), f"{model_name} is not an embedding model"
config = OpenAIEmbeddingFunctionConfig(api_key=api_key, model_name=model_name)

Type guard

def is_openai_embedding_model(name: str) -> bool:
    return name.startswith("text-embedding-")

Try / catch

try:
    memory = ChromaDBVectorMemory(config=config)
except ImportError as e:
    if "OpenAI embedding function" in str(e):
        # invalid key, missing openai pkg, or wrong model id — check e.__cause__
        raise
    raise

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/21f2b10c3061f0f3. Report an issue: GitHub.