FoundationAgents/MetaGPT · error · ValueError
To use OpenAIEmbedding, please ensure that config.llm.api_ty
Error message
To use OpenAIEmbedding, please ensure that config.llm.api_type is correctly set to 'openai'.
What it means
Raised by metagpt.utils.embedding.get_embedding(): it asks the global config for an OpenAI-style LLM (config.get_openai_llm()) and that returns None because no LLM entry with api_type='openai' exists. OpenAIEmbedding can only be constructed from an 'openai' api_type entry, so any other or missing configuration aborts here.
Source
Thrown at metagpt/utils/embedding.py:16
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2024/1/4 20:58
@Author : alexanderwu
@File : embedding.py
"""
from llama_index.embeddings.openai import OpenAIEmbedding
from metagpt.config2 import config
def get_embedding() -> OpenAIEmbedding:
llm = config.get_openai_llm()
if llm is None:
raise ValueError("To use OpenAIEmbedding, please ensure that config.llm.api_type is correctly set to 'openai'.")
embedding = OpenAIEmbedding(api_key=llm.api_key, api_base=llm.base_url)
return embedding
View on GitHub (pinned to 11cdf466d0)
Solutions
- Set an OpenAI entry in your config, e.g. in config2.yaml: llm: {api_type: 'openai', base_url: ..., api_key: ...}, so config.get_openai_llm() finds it.
- If you use another provider, supply embeddings differently (configure a custom llama_index embedding) instead of relying on get_embedding().
- Verify with `config.get_openai_llm() is not None` before invoking RAG code paths.
- Check that the config file passed via --config/--project-config is actually the one being loaded.
Example fix
# before (config2.yaml has no openai llm)
from metagpt.utils.embedding import get_embedding
emb = get_embedding() # ValueError
# after: config2.yaml
llm:
api_type: 'openai'
base_url: 'https://api.openai.com/v1'
api_key: '${OPENAI_API_KEY}' Defensive patterns
Strategy: validation
Validate before calling
from metagpt.config2 import config
if config.get_openai_llm() is None:
raise SystemExit('Configure llm.api_type=openai before running RAG features') Type guard
def has_openai_llm() -> bool:
return config.get_openai_llm() is not None Try / catch
try:
emb = get_embedding()
except ValueError as e:
if 'api_type' in str(e):
logger.error('Missing openai llm config; skipping embedding-dependent step')
emb = None # or use a local embedding model as fallback Prevention
- Keep one canonical config2.yaml with an llm block of api_type 'openai' in repos that use RAG.
- Assert config.get_openai_llm() during startup, not lazily at first embedding call.
- Use ${OPENAI_API_KEY} env substitution instead of hardcoding keys.
When it happens
Trigger: Calling get_embedding() when config.llm.api_type is unset, or set to a non-openai provider ('azure', 'ollama', 'anthropic', ...), or when using a config2 YAML/key setup where the llm block is missing. Typically reached via RAG flows (KnowledgeStorage / rag routes) that need embeddings.
Common situations: Running MetaGPT's RAG features with only a non-OpenAI LLM configured; forgetting the llm section in metaagpt config2.yaml; environment where OPENAI_API_KEY was set but the structured config still lacks api_type: openai.
Related errors
- get_embedding failed
- To use RAG, please set your embedding in config2.yaml.
- The embedding type is currently not supported: `{type(key)}`
- Missing fields: {missing_fields}
- Please set your API key in {root_config_path}. If you also s
AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14).
Data as JSON: /api/errors/74e78b0967cb77e5.
Report an issue: GitHub.