Skip to content

MistralAiApi fails to parse thinking content from Magistral reasoning models #5019

@KyleKreuter

Description

@KyleKreuter

Bug description
When using Mistral's Magistral reasoning models (e.g., magistral-medium-2509), the ChatCompletionMessage.content() method throws an IllegalStateException: The content is not a string! because Magistral models return the content field as an array of content blocks instead of a plain string.

The reasoning models return a structured response with separate thinking and text blocks, but the current implementation only handles string content.

Environment

  • Spring AI version: 1.0.x / 1.1.0-Mx
  • Java version: 21
  • Mistral model: magistral-medium-latest (magistral-medium-2509)

Steps to reproduce

  1. Configure Spring AI with Mistral AI provider
  2. Use a Magistral reasoning model (magistral-medium-latest or magistral-small latest)
  3. Send any chat completion request
  4. Call .content() on the response message

Expected behavior
The content() method should extract and return the final answer from the text content block, ignoring the thinking block.

Minimal Complete Reproducible example

  {
    "choices": [
      {
        "message": {
          "role": "assistant",
          "content": [
            {
              "type": "thinking",
              "thinking": [
                {
                  "type": "text",
                  "text": "Reasoning traces..."
                }
              ]
            },
            {
              "type": "text",
              "text": "Final answer..."
            }
          ]
        }
      }
    ]
  }

Leads to:
Caused by: java.lang.IllegalStateException: The content is not a string!

org.springframework.ai.mistralai.api.MistralAiApi$ChatCompletionMessage.content(MistralAiApi.java:~1425)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions