huggingface/smolagents · error · AgentGenerationError

Error in generating model output: {e}

Error message

Error in generating model output:
{e}

What it means

CodeAgent._step_stream wraps any exception from the model's generation call (which produces the code-action text) in AgentGenerationError. The chained cause carries the real failure: provider auth/rate-limit errors, network issues, or malformed provider responses.

Source

Thrown at src/smolagents/agents.py:1700

                memory_step.model_output_message = chat_message
                output_text = chat_message.content
                self.logger.log_markdown(
                    content=output_text or "",
                    title="Output message of the LLM:",
                    level=LogLevel.DEBUG,
                )

            if not self._use_structured_outputs_internally:
                # This adds the end code sequence (i.e. the closing code block tag) to the history.
                # This will nudge subsequent LLM calls to finish with this end code sequence, thus efficiently stopping generation.
                if output_text and not output_text.strip().endswith(self.code_block_tags[1]):
                    output_text += self.code_block_tags[1]
                    memory_step.model_output_message.content = output_text

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

        ### Parse output ###
        try:
            if self._use_structured_outputs_internally:
                code_action = json.loads(output_text)["code"]
                code_action = extract_code_from_text(code_action, self.code_block_tags) or code_action
            else:
                code_action = parse_code_blobs(output_text, self.code_block_tags)
            code_action = fix_final_answer_code(code_action)
            memory_step.code_action = code_action
        except Exception as e:
            error_msg = f"Error in code parsing:\n{e}\nMake sure to provide correct code blobs."
            raise AgentParsingError(error_msg, self.logger)

        tool_call = ToolCall(
            name="python_interpreter",
            arguments=code_action,
            id=f"call_{len(self.memory.steps)}",

View on GitHub (pinned to 30bb116109)

Solutions

  1. Read the chained exception (from e) to find the underlying provider error and fix it (key, quota, base_url).
  2. Add retry with backoff around agent.run for transient 429/5xx errors.
  3. Pin compatible smolagents/provider SDK versions if the cause is response parsing.
Defensive patterns

Strategy: retry

Try / catch

from smolagents.exceptions import AgentGenerationError

try:
    result = agent.run(task)
except AgentGenerationError as e:
    if isinstance(e.__cause__, (ConnectionError, TimeoutError)):
        time.sleep(10)
        result = agent.run(task)
    else:
        raise

Prevention

When it happens

Trigger: agent.run()/step() with a CodeAgent where model.generate() raises mid-run — invalid API key, 429/5xx from the provider, timeout, or response schema mismatch.

Common situations: Long autonomous runs hitting token limits; expired credentials; flaky network to the LLM provider; provider API contract changes across library versions.

Related errors


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