FoundationAgents/MetaGPT · error · ValueError

get_embedding failed

Error message

get_embedding failed

What it means

In the Stanford Town module, get_embedding calls OpenAI embeddings and retries 3 times (5s apart) on any exception; if all retries fail and no embedding was ever produced, it raises ValueError('get_embedding failed'). The per-attempt exceptions are only logged at info level, so the raise itself hides the root cause (bad API key, wrong model, network).

Source

Thrown at metagpt/ext/stanford_town/utils/utils.py:64

                analysis_list += [row]
        return analysis_list[0], analysis_list[1:]


def get_embedding(text, model: str = "text-embedding-ada-002"):
    text = text.replace("\n", " ")
    embedding = None
    if not text:
        text = "this is blank"
    for idx in range(3):
        try:
            embedding = (
                OpenAI(api_key=config.llm.api_key).embeddings.create(input=[text], model=model).data[0].embedding
            )
        except Exception as exp:
            logger.info(f"get_embedding failed, exp: {exp}, will retry.")
            time.sleep(5)
    if not embedding:
        raise ValueError("get_embedding failed")
    return embedding


def extract_first_json_dict(data_str: str) -> Union[None, dict]:
    # Find the first occurrence of a JSON object within the string
    start_idx = data_str.find("{")
    end_idx = data_str.find("}", start_idx) + 1

    # Check if both start and end indices were found
    if start_idx == -1 or end_idx == 0:
        return None

    # Extract the first JSON dictionary
    json_str = data_str[start_idx:end_idx]

    try:
        # Attempt to parse the JSON data
        json_dict = json.loads(json_str)

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Enable info logs (or add logging of exp) to see the actual per-attempt exception before the final raise.
  2. Verify config.llm.api_key is set and valid, and that the embedding model name is one your key can access.
  3. Check network/proxy connectivity to the OpenAI embeddings endpoint.
  4. For rate limits, slow down simulation or increase retry backoff.

Example fix

// before
embedding = OpenAI(api_key=config.llm.api_key).embeddings.create(input=[text], model=model).data[0].embedding

// after (surface root cause)
client = OpenAI(api_key=config.llm.api_key)
try:
    embedding = client.embeddings.create(input=[text], model=model).data[0].embedding
except Exception as exp:
    logger.error(f"get_embedding failed permanently: {exp}")
    raise
Defensive patterns

Strategy: retry

Validate before calling

from metagpt.config2 import config

def embedding_ready(model: str) -> bool:
    return bool(config.llm.api_key)  # cheap precondition; full check needs a live call

Try / catch

from metagpt.ext.stanford_town.utils.utils import get_embedding

async def safe_embedding(text: str, model: str, attempts: int = 3):
    last = None
    for _ in range(attempts):
        try:
            return get_embedding(text, model=model)
        except ValueError as e:
            if "get_embedding failed" in str(e):
                last = e
                continue
            raise
    raise RuntimeError("embeddings unavailable; check api key/model/network") from last

Prevention

When it happens

Trigger: Invalid/missing OpenAI API key in config.llm.api_key; embedding model not available to the account; network/proxy blocking api.openai.com; rate limits persisting across all 3 retries.

Common situations: Running stanford_town examples without OPENAI_API_KEY configured; using a key without access to the requested embedding model; corporate proxies or offline environments; exceeding rate limits during large simulations.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/3136b9f7a98472c7. Report an issue: GitHub.