{"record":{"id":"49d39092ca306b9b","repo":"agentscope-ai/agentscope","slug":"failed-to-generate-structured-output-for-model","errorCode":null,"errorMessage":"Failed to generate structured output for model.","messagePattern":"Failed to generate structured output for model\\.","errorType":"exception","errorClass":"StructuredOutputError","httpStatus":null,"severity":"error","filePath":"src/agentscope/model/_base.py","lineNumber":709,"sourceCode":"\n        if completed_response is None or not completed_response.content:\n            raise StructuredOutputError(\n                f\"Failed to get the completed response from model \"\n                f\"{model_name}.\",\n            )\n\n        structured_output: dict[str, Any] | None = None\n        try:\n            for _ in completed_response.content:\n                if isinstance(_, ToolCallBlock) and _.name == func_name:\n                    structured_output = _json_loads_with_repair(\n                        _.input,\n                        input_schema,\n                    )\n                    break\n\n            if structured_output is None:\n                raise StructuredOutputError(\n                    \"Failed to generate structured output for model.\",\n                )\n\n            # Validate the output\n            if isinstance(structured_model, dict):\n                jsonschema.validate(structured_output, structured_model)\n\n            elif issubclass(structured_model, BaseModel):\n                structured_model.model_validate(structured_output)\n\n            else:\n                raise ValueError(\n                    \"The structured_model is expected to be a subclass of \"\n                    \"Pydantic.BaseModel or a dict, \"\n                    f\"but got {type(structured_model)}.\",\n                )\n        except (\n            ToolJSONDecodeError,","sourceCodeStart":691,"sourceCodeEnd":727,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/model/_base.py#L691-L727","documentation":"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.","triggerScenarios":"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.","commonSituations":"Weaker models ignoring the schema instruction; prompt not clearly requesting JSON; schema so complex the model hallucinates format; temperature too high.","solutions":["Make the prompt explicitly demand raw JSON matching the schema, with no surrounding text","Lower temperature (e.g. 0) for deterministic formatting","Use a stronger model or one with native JSON mode so the strategy is enforced server-side","Retry — intermittent format drift is common; agentscope's strategy fallbacks may succeed on a second attempt"],"exampleFix":"# before\nmsgs = [Msg('user', 'What are the fields?')]\n\n# after\nmsgs = [Msg('user', 'Return ONLY a JSON object with keys \"name\" and \"age\". No other text.')]","handlingStrategy":"retry","validationCode":"prompt = 'Return ONLY a JSON object matching this schema, no other text: ' + json.dumps(Schema.model_json_schema())","typeGuard":null,"tryCatchPattern":"except StructuredOutputError:\n    # feed back and retry with stricter instruction\n    msgs.append(Msg('user', 'Your last reply was not valid JSON. Reply with ONLY the JSON object.'))","preventionTips":["Use temperature 0 for structured extraction","Prefer models with native JSON mode","Include the schema in the prompt"],"tags":["agentscope","structured-output","json-parsing","model-output"],"backgroundTag":"invalid-json-from-llm","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}