Skip to content

[BUG MODEL]: magistral-medium-latest returns erratic ThinkChunks and TextChunks #252

@smathot

Description

@smathot

Model

magistral-medium-latest

Expected Behavior

I expect a single ThinkChunk and a single TextChunk in the content as indicated on the documentation. However, what I think I'm seeing instead is that the TextChunk is itself split up into three parts: a TextChunk, an empty ThinkChunk, and another TextChunk, resulting in four chunks in total.

(The tool_calls part seems fine.)

Additional Context

This occurs in the context of SigmundAI when using magistral-medium-latest.

Suggested Solutions

My hunch is that the model response is parsed incorrectly server-side because of the workspace tags that are included in the response.

Request Payload

[{'content': 'You are Sigmund, a brilliant AI assistant. You always put code '
             'between triple backticks (```), and never use triple backticks '
             'for anything else. You sometimes use emojis.\n'
             '\n'
             'When providing examples or updated text or code to the user '
             'always do this through the workspace. You set workspace content '
             'by including `<workspace language="language">text or '
             'code</workspace>` in your reply or by using tools.\n'
             '\n'
             'Here is an example:\n'
             '\n'
             '```\n'
             'Sure, I can write a hello world function for you! I added the '
             'code to the workspace.\n'
             '\n'
             '<workspace language="python">\n'
             'def hello_world():\n'
             "    print('Hello world!')\n"
             '</workspace>\n'
             '```\n'
             '\n'
             'Available languages: css, html, javascript, opensesame, python, '
             'r, markdown\n'
             '\n'
             'Important: always use the workspace as shown above, and do *not* '
             'include long examples of text or code in your reply.\n',
  'role': 'system'},
 {'content': 'Can you show me how to calculate the square root of 7 in Python?',
  'role': 'user'}]

Output

ChatCompletionResponse(
    id="06ccb1bf42a44619b13b83678b95ca22",
    object="chat.completion",
    model="magistral-medium-latest",
    usage=UsageInfo(
        prompt_tokens=339,
        completion_tokens=245,
        total_tokens=584,
        prompt_audio_seconds=Unset(),
    ),
    created=1753539878,
    choices=[
        ChatCompletionChoice(
            index=0,
            message=AssistantMessage(
                content=[
                    ThinkChunk(
                        thinking=[
                            TextChunk(
                                text="Alright, I need to show how to calculate the square root of 7 in Python. I remember that Python has a module called `math` that contains a function for calculating square roots. The function is called `sqrt`.\n\nFirst, I need to import the `math` module. Then, I can use the `sqrt` function to calculate the square root of 7.\n\nHere's how I can do it:\n\n```python\nimport math\nresult = math.sqrt(7)\nprint(result)\n```\n\nBut before I show this to the user, I should test it to make sure it works correctly. I can use the `execute_code` tool to run this code and see the output.",
                                type="text",
                            )
                        ],
                        closed=None,
                        type="thinking",
                    ),
                    TextChunk(
                        text='Sure! I can show you how to calculate the square root of 7 in Python. Here\'s the code:\n\n```python\nimport math\nresult = math.sqrt(7)\nprint(result)\n```\n\nLet me execute this code to show you the result.\n\n<workspace language="python">\nimport math\nresult = math.sqrt(7)\nprint(result)\n',
                        type="text",
                    ),
                    ThinkChunk(thinking=[], closed=None, type="thinking"),
                    TextChunk(text="workspace>\n\n", type="text"),
                ],
                tool_calls=[
                    ToolCall(
                        function=FunctionCall(
                            name="execute_code",
                            arguments='{"language": "python", "code": "import math\\nresult = math.sqrt(7)\\nprint(result)"}',
                        ),
                        id="y7FYBCEod",
                        type=None,
                        index=0,
                        metadata=Unset(),
                    )
                ],
                prefix=False,
                role="assistant",
            ),
            finish_reason="tool_calls",
        )
    ],
)
```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions