# Structured output: each strategy

Recorded on 2026-11-14 on the x86 server guest with Ollama 0.31.1 on CPU. Models: qwen3:4b-instruct-2507-q4_K_M. ChatOllama settings: temperature 0, seed 42, num_ctx 16384, num_predict 512.

## a bare schema on a model with no profile: AutoStrategy picks ToolStrategy

structured_response:
{
  "value": 5,
  "unit": ""
}

structured_response type: Answer

[
  {
    "type": "AIMessage",
    "content": "",
    "id": "lc_run--<uuid-1>-0",
    "tool_calls": [
      {
        "name": "Answer",
        "args": {
          "value": 5,
          "unit": ""
        },
        "id": "<uuid-2>",
        "type": "tool_call"
      }
    ],
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 164,
      "prompt_eval_duration": "<ns>",
      "eval_count": 24,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 164,
      "output_tokens": 24,
      "total_tokens": 188
    }
  },
  {
    "type": "ToolMessage",
    "content": "Returning structured response: value=5 unit=''",
    "name": "Answer",
    "id": "<uuid-3>",
    "tool_call_id": "<uuid-2>",
    "status": "success"
  }
]

model calls, as the model received them:
[
  {
    "model": "no-profile",
    "call": 1,
    "messages": [
      "human: What is 2 + 3?"
    ],
    "tools": [
      "Answer"
    ]
  }
]

## a bare schema on a model whose profile says structured_output: AutoStrategy picks ProviderStrategy

structured_response:
{
  "value": 5,
  "unit": "none"
}

structured_response type: Answer

[
  {
    "type": "AIMessage",
    "content": "{\"value\": 5}",
    "id": "lc_run--<uuid-4>-0",
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 16,
      "prompt_eval_duration": "<ns>",
      "eval_count": 14,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 16,
      "output_tokens": 14,
      "total_tokens": 30
    }
  }
]

model calls, as the model received them:
[
  {
    "model": "profiled",
    "call": 1,
    "messages": [
      "human: What is 2 + 3?"
    ],
    "format": "json schema"
  }
]

## AutoStrategy written out

structured_response:
{
  "value": 5,
  "unit": ""
}

structured_response type: Answer

[
  {
    "type": "AIMessage",
    "content": "",
    "id": "lc_run--<uuid-5>-0",
    "tool_calls": [
      {
        "name": "Answer",
        "args": {
          "value": 5,
          "unit": ""
        },
        "id": "<uuid-6>",
        "type": "tool_call"
      }
    ],
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 164,
      "prompt_eval_duration": "<ns>",
      "eval_count": 24,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 164,
      "output_tokens": 24,
      "total_tokens": 188
    }
  },
  {
    "type": "ToolMessage",
    "content": "Returning structured response: value=5 unit=''",
    "name": "Answer",
    "id": "<uuid-7>",
    "tool_call_id": "<uuid-6>",
    "status": "success"
  }
]

model calls, as the model received them:
[
  {
    "model": "auto",
    "call": 1,
    "messages": [
      "human: What is 2 + 3?"
    ],
    "tools": [
      "Answer"
    ]
  }
]

## ProviderStrategy on ChatOllama

structured_response:
{
  "value": 5,
  "unit": "none"
}

structured_response type: Answer

[
  {
    "type": "AIMessage",
    "content": "{\"value\": 5}",
    "id": "lc_run--<uuid-8>-0",
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 16,
      "prompt_eval_duration": "<ns>",
      "eval_count": 14,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 16,
      "output_tokens": 14,
      "total_tokens": 30
    }
  }
]

model calls, as the model received them:
[
  {
    "model": "provider",
    "call": 1,
    "messages": [
      "human: What is 2 + 3?"
    ],
    "format": "json schema"
  }
]

## ToolStrategy with a TypedDict

invoke:

raised ollama._types.ResponseError: {"error":{"code":400,"message":"Cannot have 2 or more assistant messages at the end of the list.","type":"invalid_request_error"}} (status code: 400)

model calls, as the model received them:
[
  {
    "model": "typeddict",
    "call": 1,
    "messages": [
      "human: What is 2 + 3?"
    ],
    "tools": [
      "AnswerDict"
    ]
  },
  {
    "model": "typeddict",
    "call": 2,
    "messages": [
      "human: What is 2 + 3?",
      "ai: 2 + 3 = 5."
    ],
    "tools": [
      "AnswerDict"
    ]
  },
  {
    "model": "typeddict",
    "call": 3,
    "messages": [
      "human: What is 2 + 3?",
      "ai: 2 + 3 = 5.",
      "ai: 2 + 3 = 5."
    ],
    "tools": [
      "AnswerDict"
    ]
  }
]

## ToolStrategy with a dataclass

invoke:

raised ollama._types.ResponseError: {"error":{"code":400,"message":"Cannot have 2 or more assistant messages at the end of the list.","type":"invalid_request_error"}} (status code: 400)

model calls, as the model received them:
[
  {
    "model": "dataclass",
    "call": 1,
    "messages": [
      "human: What is 2 + 3?"
    ],
    "tools": [
      "AnswerData"
    ]
  },
  {
    "model": "dataclass",
    "call": 2,
    "messages": [
      "human: What is 2 + 3?",
      "ai: 2 + 3 = 5."
    ],
    "tools": [
      "AnswerData"
    ]
  },
  {
    "model": "dataclass",
    "call": 3,
    "messages": [
      "human: What is 2 + 3?",
      "ai: 2 + 3 = 5.",
      "ai: 2 + 3 = 5."
    ],
    "tools": [
      "AnswerData"
    ]
  }
]

## ToolStrategy with a raw JSON schema dict: returned without validation

structured_response:
{
  "value": 5
}

structured_response type: dict

[
  {
    "type": "AIMessage",
    "content": "",
    "id": "lc_run--<uuid-9>-0",
    "tool_calls": [
      {
        "name": "answer_schema",
        "args": {
          "value": 5
        },
        "id": "<uuid-10>",
        "type": "tool_call"
      }
    ],
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 167,
      "prompt_eval_duration": "<ns>",
      "eval_count": 20,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 167,
      "output_tokens": 20,
      "total_tokens": 187
    }
  },
  {
    "type": "ToolMessage",
    "content": "Returning structured response: {'value': 5}",
    "name": "answer_schema",
    "id": "<uuid-11>",
    "tool_call_id": "<uuid-10>",
    "status": "success"
  }
]

model calls, as the model received them:
[
  {
    "model": "json-schema",
    "call": 1,
    "messages": [
      "human: What is 2 + 3? Give the value as the English word, for example five, not as digi"
    ],
    "tools": [
      "answer_schema"
    ]
  }
]

## ToolStrategy with a union: one tool per schema

structured_response:
{
  "value": 5,
  "unit": ""
}

structured_response type: Answer

[
  {
    "type": "AIMessage",
    "content": "",
    "id": "lc_run--<uuid-12>-0",
    "tool_calls": [
      {
        "name": "Answer",
        "args": {
          "value": 5,
          "unit": ""
        },
        "id": "<uuid-13>",
        "type": "tool_call"
      }
    ],
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 213,
      "prompt_eval_duration": "<ns>",
      "eval_count": 24,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 213,
      "output_tokens": 24,
      "total_tokens": 237
    }
  },
  {
    "type": "ToolMessage",
    "content": "Returning structured response: value=5 unit=''",
    "name": "Answer",
    "id": "<uuid-14>",
    "tool_call_id": "<uuid-13>",
    "status": "success"
  }
]

model calls, as the model received them:
[
  {
    "model": "union",
    "call": 1,
    "messages": [
      "human: What is 2 + 3?"
    ],
    "tools": [
      "Answer",
      "Refusal"
    ]
  }
]

## ToolStrategy with the add tool bound: the model may answer in text

structured_response:
null

structured_response type: NoneType

[
  {
    "type": "AIMessage",
    "content": "",
    "id": "lc_run--<uuid-15>-0",
    "tool_calls": [
      {
        "name": "add",
        "args": {
          "a": 2,
          "b": 3
        },
        "id": "<uuid-16>",
        "type": "tool_call"
      }
    ],
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 228,
      "prompt_eval_duration": "<ns>",
      "eval_count": 25,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 228,
      "output_tokens": 25,
      "total_tokens": 253
    }
  },
  {
    "type": "ToolMessage",
    "content": "5",
    "name": "add",
    "id": "<uuid-17>",
    "tool_call_id": "<uuid-16>",
    "status": "success"
  },
  {
    "type": "AIMessage",
    "content": "The result of 2 + 3 is 5.\n\nAnswer: 5",
    "id": "lc_run--<uuid-18>-0",
    "response_metadata": {
      "model": "qwen3:4b-instruct-2507-q4_K_M",
      "created_at": "<time>",
      "done": true,
      "done_reason": "stop",
      "total_duration": "<ns>",
      "load_duration": "<ns>",
      "prompt_eval_count": 267,
      "prompt_eval_duration": "<ns>",
      "eval_count": 17,
      "eval_duration": "<ns>",
      "logprobs": null,
      "model_name": "qwen3:4b-instruct-2507-q4_K_M",
      "model_provider": "ollama"
    },
    "usage_metadata": {
      "input_tokens": 267,
      "output_tokens": 17,
      "total_tokens": 284
    }
  }
]

model calls, as the model received them:
[
  {
    "model": "tool-and-text",
    "call": 1,
    "messages": [
      "human: What is 2 + 3?"
    ],
    "tools": [
      "add",
      "Answer"
    ]
  },
  {
    "model": "tool-and-text",
    "call": 2,
    "messages": [
      "human: What is 2 + 3?",
      "ai: calls add",
      "tool: 5"
    ],
    "tools": [
      "add",
      "Answer"
    ]
  }
]

## ProviderStrategy with the add tool bound

invoke:

raised langchain.agents.structured_output.StructuredOutputValidationError: Failed to parse structured output for tool 'Answer': Native structured output expected valid JSON for Answer, but parsing failed: Expecting value: line 1 column 1 (char 0)..

model calls, as the model received them:
[
  {
    "model": "provider-and-tool",
    "call": 1,
    "messages": [
      "human: What is 2 + 3?"
    ],
    "tools": [
      "add"
    ],
    "format": "json schema"
  }
]
