agentscope-ai/agentscope · error · StructuredOutputError

Failed to generate structured output for model.

Error message

Failed to generate structured output for model.

What it means

The model call succeeded but none of the extraction paths (JSON parsing of content, tool-call arguments parsing, etc.) produced a structured_output dict, so agentscope raises StructuredOutputError. The response content didn't contain parseable JSON in any recognized location.

Source

Thrown at src/agentscope/model/_base.py:709

        if completed_response is None or not completed_response.content:
            raise StructuredOutputError(
                f"Failed to get the completed response from model "
                f"{model_name}.",
            )

        structured_output: dict[str, Any] | None = None
        try:
            for _ in completed_response.content:
                if isinstance(_, ToolCallBlock) and _.name == func_name:
                    structured_output = _json_loads_with_repair(
                        _.input,
                        input_schema,
                    )
                    break

            if structured_output is None:
                raise StructuredOutputError(
                    "Failed to generate structured output for model.",
                )

            # Validate the output
            if isinstance(structured_model, dict):
                jsonschema.validate(structured_output, structured_model)

            elif issubclass(structured_model, BaseModel):
                structured_model.model_validate(structured_output)

            else:
                raise ValueError(
                    "The structured_model is expected to be a subclass of "
                    "Pydantic.BaseModel or a dict, "
                    f"but got {type(structured_model)}.",
                )
        except (
            ToolJSONDecodeError,

View on GitHub (pinned to e90f1c7592)

Solutions

  1. Make the prompt explicitly demand raw JSON matching the schema, with no surrounding text
  2. Lower temperature (e.g. 0) for deterministic formatting
  3. Use a stronger model or one with native JSON mode so the strategy is enforced server-side
  4. Retry — intermittent format drift is common; agentscope's strategy fallbacks may succeed on a second attempt

Example fix

# before
msgs = [Msg('user', 'What are the fields?')]

# after
msgs = [Msg('user', 'Return ONLY a JSON object with keys "name" and "age". No other text.')]
Defensive patterns

Strategy: retry

Validate before calling

prompt = 'Return ONLY a JSON object matching this schema, no other text: ' + json.dumps(Schema.model_json_schema())

Try / catch

except StructuredOutputError:
    # feed back and retry with stricter instruction
    msgs.append(Msg('user', 'Your last reply was not valid JSON. Reply with ONLY the JSON object.'))

Prevention

When it happens

Trigger: Model returns prose instead of JSON, wraps JSON in markdown fences the parser doesn't handle, or returns a tool call with empty/malformed arguments.

Common situations: Weaker models ignoring the schema instruction; prompt not clearly requesting JSON; schema so complex the model hallucinates format; temperature too high.

Related errors


AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28). Data as JSON: /api/errors/49d39092ca306b9b. Report an issue: GitHub.