huggingface/smolagents · error · AgentGenerationError

Error while generating output

Error message

Error while generating output:
{e}

What it means

Inside ToolCallingAgent._step_stream, any exception raised by model.generate() while producing the chat completion is wrapped in AgentGenerationError. The original exception is chained (raise ... from e), so the traceback retains the root cause — typically provider API errors, auth failures, rate limits, or malformed responses.

Solutions

  1. Inspect the chained cause (`raise ... from e` — read the full traceback) to identify the underlying provider error.
  2. Fix the root cause: valid API key, correct base_url, sufficient quota/limits.
  3. Wrap agent.run in retry logic (e.g. tenacity) for transient 429/5xx provider errors.
Defensive patterns

Strategy: retry

Try / catch

from smolagents.exceptions import AgentGenerationError

try:
    result = agent.run(task)
except AgentGenerationError as e:
    cause = e.__cause__  # inspect underlying provider error
    if 'rate limit' in str(cause).lower():
        time.sleep(30)
        result = agent.run(task)
    else:
        raise

Prevention

When it happens

Trigger: Running agent.run(...) / agent.step(...) with stream_outputs where self.model.generate() raises: expired API key, network timeout, 429 rate limit, or a provider returning an unexpected payload.

Common situations: Long agent runs that exhaust token/rate limits; invalid or missing OPENAI_API_KEY-style env vars; transient network failures to the LLM provider; provider API schema changes between library versions.

Related errors


AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28). Data as JSON: /api/errors/8ebf795f76aa4389. Report an issue: GitHub.

Appendix: source

Thrown at src/smolagents/agents.py:1325

                chat_message = agglomerate_stream_deltas(chat_message_stream_deltas)
            else:
                chat_message: ChatMessage = self.model.generate(
                    input_messages,
                    stop_sequences=["Observation:", "Calling tools:"],
                    tools_to_call_from=self.tools_and_managed_agents,
                )
                self.logger.log_markdown(
                    content=str(chat_message.content or chat_message.raw or ""),
                    title="Output message of the LLM:",
                    level=LogLevel.DEBUG,
                )

            # Record model output
            memory_step.model_output_message = chat_message
            memory_step.model_output = chat_message.content
            memory_step.token_usage = chat_message.token_usage
        except Exception as e:
            raise AgentGenerationError(f"Error while generating output:\n{e}", self.logger) from e

        if chat_message.tool_calls is None or len(chat_message.tool_calls) == 0:
            try:
                chat_message = self.model.parse_tool_calls(chat_message)
            except Exception as e:
                raise AgentParsingError(f"Error while parsing tool call from model output: {e}", self.logger)
        else:
            for tool_call in chat_message.tool_calls:
                tool_call.function.arguments = parse_json_if_needed(tool_call.function.arguments)
        final_answer, got_final_answer = None, False
        for output in self.process_tool_calls(chat_message, memory_step):
            yield output
            if isinstance(output, ToolOutput):
                if output.is_final_answer:
                    if len(chat_message.tool_calls) > 1:
                        raise AgentExecutionError(
                            "If you want to return an answer, please do not perform any other tool calls than the final answer tool call!",
                            self.logger,

View on GitHub (pinned to 30bb116109)