{"record":{"id":"74e78b0967cb77e5","repo":"FoundationAgents/MetaGPT","slug":"to-use-openaiembedding-please-ensure-that-config","errorCode":null,"errorMessage":"To use OpenAIEmbedding, please ensure that config.llm.api_type is correctly set to 'openai'.","messagePattern":"To use OpenAIEmbedding, please ensure that config\\.llm\\.api_type is correctly set to 'openai'\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/embedding.py","lineNumber":16,"sourceCode":"#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n\"\"\"\n@Time    : 2024/1/4 20:58\n@Author  : alexanderwu\n@File    : embedding.py\n\"\"\"\nfrom llama_index.embeddings.openai import OpenAIEmbedding\n\nfrom metagpt.config2 import config\n\n\ndef get_embedding() -> OpenAIEmbedding:\n    llm = config.get_openai_llm()\n    if llm is None:\n        raise ValueError(\"To use OpenAIEmbedding, please ensure that config.llm.api_type is correctly set to 'openai'.\")\n\n    embedding = OpenAIEmbedding(api_key=llm.api_key, api_base=llm.base_url)\n    return embedding\n","sourceCodeStart":1,"sourceCodeEnd":20,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/embedding.py#L1-L20","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before (config2.yaml has no openai llm)\nfrom metagpt.utils.embedding import get_embedding\nemb = get_embedding()  # ValueError\n\n# after: config2.yaml\nllm:\n  api_type: 'openai'\n  base_url: 'https://api.openai.com/v1'\n  api_key: '${OPENAI_API_KEY}'","handlingStrategy":"validation","validationCode":"from metagpt.config2 import config\nif config.get_openai_llm() is None:\n    raise SystemExit('Configure llm.api_type=openai before running RAG features')","typeGuard":"def has_openai_llm() -> bool:\n    return config.get_openai_llm() is not None","tryCatchPattern":"try:\n    emb = get_embedding()\nexcept ValueError as e:\n    if 'api_type' in str(e):\n        logger.error('Missing openai llm config; skipping embedding-dependent step')\n        emb = None  # or use a local embedding model as fallback","preventionTips":["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."],"tags":["config","embedding","rag","openai"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}