assafelovic/gpt-researcher · critical · RuntimeError

Failed to get response from {llm_provider} API

Error message

Failed to get response from {llm_provider} API

What it means

After create_chat_completion exhausts its provider retry loop, it logs and raises RuntimeError chained from the last exception. This is the catch-all 'the LLM API never succeeded' error — the underlying cause (network, auth, quota, malformed request) is attached as __cause__ via `from last_exception`.

Source

Thrown at gpt_researcher/utils/llm.py:158

                continue
            break

        if cost_callback:
            llm_costs = calculate_llm_cost(
                llm_provider=llm_provider,
                model=model,
                input_content=str(messages),
                output_content=response,
                response_metadata=provider.last_response_metadata,
                usage_metadata=provider.last_usage_metadata,
                request_options=provider_kwargs,
            )
            cost_callback(llm_costs)

        return response

    logging.error(f"Failed to get response from {llm_provider} API")
    raise RuntimeError(f"Failed to get response from {llm_provider} API") from last_exception


async def construct_subtopics(
    task: str,
    data: str,
    config,
    subtopics: list = [],
    prompt_family: type[PromptFamily] | PromptFamily = PromptFamily,
    **kwargs
) -> list:
    """
    Construct subtopics based on the given task and data.

    Args:
        task (str): The main task or topic.
        data (str): Additional data for context.
        config: Configuration settings.
        subtopics (list, optional): Existing subtopics. Defaults to [].

View on GitHub (pinned to 6f998577d5)

Solutions

  1. Inspect the chained exception: `except RuntimeError as e: print(e.__cause__)` to see the real HTTP error
  2. Verify the API key (OPENAI_API_KEY or provider equivalent) and that it has quota/billing
  3. Reduce request rate or add backoff; check provider status page
  4. Pin/upgrade langchain + provider packages to versions compatible with your gpt-researcher release
  5. Catch RuntimeError in your orchestration code and fall back to a cheaper provider/model

Example fix

# before
resp = await create_chat_completion(...)  # RuntimeError: Failed to get response from openai API

# after
try:
    resp = await create_chat_completion(...)
except RuntimeError as e:
    logger.error('LLM failed: %s', e.__cause__)
    resp = await create_chat_completion(..., llm_provider='ollama')  # fallback
Defensive patterns

Strategy: fallback

Validate before calling

import os
assert os.getenv('OPENAI_API_KEY'), 'Missing OPENAI_API_KEY — LLM calls will fail'

Try / catch

try:
    resp = await create_chat_completion(prompt, model)
except RuntimeError as e:
    logger.warning('LLM provider failed: %s', e.__cause__)
    resp = await create_chat_completion(prompt, fallback_model)  # retry/fallback

Prevention

When it happens

Trigger: Every attempt to call the LLM provider failed: invalid/expired API key (401), rate limit or quota exhausted (429), model name not found, network/DNS failure, or a provider SDK incompatibility — after all internal retries are spent, this RuntimeError surfaces to callers like generate_feedback or choose_agent.

Common situations: Expired OpenAI key, hitting org rate limits during long research runs, wrong model name after a provider deprecation, corporate proxy blocking api.openai.com, or mismatched langchain/langchain-openai versions after an upgrade.

Related errors


AI-assisted analysis of assafelovic/gpt-researcher@6f998577d5 (2026-08-28). Data as JSON: /api/errors/11529b3ba52c6ecd. Report an issue: GitHub.