Troubleshooting Message Ordering Conflicts When Connecting Open-Source Reasoning Models to AI Agents

    This article explains how to troubleshoot “Message ordering conflict” errors when integrating open-source reasoning models into AI agent tools through OpenAI-compatible APIs. It covers provider configuration, how `compat` parameters affect message order, common triggers, a step-by-step diagnostic process, and correct versus incorrect request examples.

    Introduction

    More AI agent tools now use open-source large models as their default models. Some of these models support a Thinking mode, which outputs a reasoning process before the final answer and is useful for code modification, multi-step planning, and similar tasks. When integrating such models, there are many configuration options, and error messages are often not intuitive.

    Key takeaway: if you encounter a “Message ordering conflict” error, first check whether the message order in the request body sent to the platform is correct, then verify the compatibility parameters in the Provider configuration, and finally validate each item one by one. This article uses an OpenAI-compatible interface as an example to explain the integration flow, parameter boundaries, and troubleshooting methods. The commands and configurations in this article are illustrative; in actual use, follow the official documentation of the AI agent tool and model platform you are using.

    1. Boundaries and Preparation Before Integration

    First, confirm the integration method. The configuration in this article applies to the scenario of “calling a cloud API through a cloud provider’s inference service or a model aggregation platform.” For local deployment, the Base URL, model ID, and startup parameters will be different. The following table helps distinguish the two:

    Item Cloud API Local deployment (vLLM, Ollama, etc.)
    Base URL The domain provided by the platform plus the /v1 path Usually a local address; the port is determined by startup parameters
    Model ID Use the name listed in the platform documentation Use the model name loaded at local startup
    Authentication Requires an API Key Usually not required, or uses a locally configured token
    Quotas and network Subject to plan quotas and external network access Subject to local GPU memory and port usage

    Before integration, prepare the following:

    • The platform account has permission to call the corresponding model, and the plan quota is sufficient.
    • An API Key has been obtained. Store the key in environment variables or a secret management tool. Do not write it into code, public configuration files, or logs. If the key is leaked, revoke it on the platform immediately and generate a new one.
    • The local network can access the Base URL, and proxies or firewalls do not block requests.
    • The model ID must exactly match the name in the platform documentation, including case and version suffixes. Do not fill it in based on experience.

    2. Provider Configuration

    This article uses an AI agent tool that supports custom OpenAI-compatible Providers as an example. The tool name and version determine the location, file name, and activation method of the configuration file, so confirm these in the tool documentation first. The general approach is to locate the global configuration file, add a custom Provider, save the file, and then restart the tool or run a reload command. Back up the original file before making changes so you can restore it directly if something fails.

    The following is an example configuration:

    1. {
    2. "baseUrl": "https://api.example-cloud.com/v1",
    3. "apiKey": "${MODEL_API_KEY}",
    4. "api": "openai-completions",
    5. "models": [
    6. {
    7. "id": "<real model ID from the platform documentation>",
    8. "name": "advanced-code-model",
    9. "reasoning": true,
    10. "input": ["text"],
    11. "contextWindow": 131072,
    12. "maxTokens": 32768,
    13. "compat": {
    14. "supportsDeveloperRole": false
    15. }
    16. }
    17. ]
    18. }

    The meaning and applicable boundaries of each field are as follows:

    • baseUrl: The API address provided by the platform, usually ending with /v1. If you write https://api.example-cloud.com without the /v1 path, the platform often returns 404. If you add /chat/completions, it will also return 404.
    • apiKey: This is an environment variable reference, not the key itself. Before starting the tool, run a command such as export MODEL_API_KEY=your_key in the current terminal. Do not write the key value into the configuration file.
    • api: The API protocol type. Choose the protocol that matches the platform documentation. If the protocol does not match, the request format will be incorrect.
    • id: The real model ID from the platform documentation, case-sensitive. If it is incorrect, the platform usually returns an error such as “model not found.”
    • name: The name displayed in the tool interface, making it easier to select the model. If the tool documentation specifies otherwise, follow the documentation.
    • reasoning: When set to true, the tool treats the model as a reasoning model and receives reasoning content. Set it to true only when the platform documentation explicitly states that Thinking mode is supported. Setting it to true for a model that does not support reasoning may cause response parsing errors or cause extra fields to be rejected by the platform.
    • input: Supported input types. Currently set to ["text"], meaning only text is sent. If the model supports images, add the corresponding type according to the documentation; otherwise, the tool will not send image content.
    • contextWindow: The context window size in tokens, not characters. If the limit is exceeded, the tool may truncate historical messages early, or the platform may return an over-length error. Use the value in the platform documentation.
    • maxTokens: The maximum number of tokens allowed for a single response, also measured in tokens. If set too high, it may exceed the platform limit and be rejected; if set too low, the answer will be truncated. Reasoning content from reasoning models also consumes this budget, so reserve enough headroom.

    3. How compat Parameters Affect Message Order

    compat describes differences between the target platform and the standard OpenAI API. It does not change the model’s own capabilities; it only affects how the AI agent tool assembles requests.

    supportsDeveloperRole is one of the most common fields. The OpenAI protocol added the developer role to carry developer instructions. Some platforms do not recognize this role. If the tool still sends developer messages, the platform may return an error or treat the message as an invalid order. When this field is set to false, the tool uses the system role instead to send the same type of instructions.

    When setting compat, note the following:

    • The field must match the platform’s actual capabilities. If the platform supports developer but the field is set to false, unnecessary rewriting occurs, but it usually does not cause an error. If the platform does not support developer but the field is set to true, role errors are more likely.
    • Different tools support different compat fields. For unrecognized fields, the tool may ignore them or refuse to start. Follow the tool documentation.
    • After changing compat, resend a test request. Do not rely only on whether the configuration file is syntactically valid.

    4. Common Triggers of Message Ordering Conflict

    This error usually indicates that the message sequence in the request does not meet the platform’s requirements. The exact error text varies by platform. The following are common causes; compare them one by one with your own logs during troubleshooting.

    1. system or developer messages are not at the beginning of the sequence, or they appear in the middle of a multi-turn conversation.
    2. tool messages (tool results) do not immediately follow the assistant message that issued the call.
    3. The platform requires user and assistant messages to alternate, and two consecutive messages with the same role are rejected.
    4. The platform does not recognize the developer role, but the request still uses developer.
    5. In multi-turn conversations, relaying historical messages may change the order, especially for assistant messages that contain reasoning content.

    Judge based on the specific platform requirements rather than applying generic rules. Some platforms allow multiple consecutive user messages, while others do not.

    5. Troubleshooting Steps

    Follow four steps in order; do not reverse them.

    Step 1: Collect logs

    Enable the tool’s debug or verbose logging mode and reproduce the error once. Record the following information:

    • The complete error text and HTTP status code.
    • The time when the request occurred.
    • The actual model ID and Base URL in use.
    • The request body sent to the platform, that is, the messages array in the log.

    Logs may contain keys and conversation content. Before sharing or saving them, remove keys and redact sensitive content.

    Step 2: Check the request body

    In the messages array of the request body, list the role of each message in order, for example system → user → assistant → tool → user. Compare this with the triggers in the previous section and identify positions that do not meet the requirements. This step usually locates the problem directly.

    Step 3: Validate item by item

    Use the following checklist to confirm whether the configuration and request are compliant.

    Check item Correct state Passed
    Model ID Exactly matches the platform documentation
    baseUrl Ends with /v1 and has no extra path
    API Key The environment variable is effective in the terminal that starts the tool
    system and developer messages Appear only at the beginning of the sequence
    tool messages Immediately follow the corresponding assistant call
    Role alternation Meets platform requirements, such as alternating user and assistant
    supportsDeveloperRole Matches the platform’s actual capabilities
    maxTokens Does not exceed the platform limit

    Step 4: Roll back and validate one item at a time

    Change only one configuration item at a time, then resend the test request. If multiple items are changed at once, you cannot determine which one actually fixed the issue. After the problem is resolved, keep the effective change and record what was modified for future reference.

    6. Correct and Incorrect Examples

    The following are two request bodies for the same conversation.

    Incorrect example:

    1. {
    2. "model": "<real model ID from the platform documentation>",
    3. "messages": [
    4. {"role": "user", "content": "Fix the boundary error in this function"},
    5. {"role": "system", "content": "You are a code assistant"},
    6. {"role": "developer", "content": "Output only the modified code"}
    7. ]
    8. }

    This request has two problems. First, the system message appears after the user message and is not at the beginning of the sequence. Second, the developer role is not supported by the target platform.

    Correct example:

    1. {
    2. "model": "<real model ID from the platform documentation>",
    3. "messages": [
    4. {"role": "system", "content": "You are a code assistant. Output only the modified code."},
    5. {"role": "user", "content": "Fix the boundary error in this function"}
    6. ]
    7. }

    The fix is to merge the two instructions into one system message, place it first in the sequence, and stop using the developer role.

    Tool-calling scenarios also need order checks. The following is incorrect because a user message is inserted between the tool result and the assistant message that initiated the call:

    1. [
    2. {"role": "assistant", "tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "read_file", "arguments": "{}"}}]},
    3. {"role": "user", "content": "Continue"},
    4. {"role": "tool", "tool_call_id": "call_1", "content": "File content"}
    5. ]

    The correct approach is to place the tool message immediately after the assistant call message, and then append the new user message.

    7. Verification and Rollback After Changes

    After making changes, first send the shortest possible test question to confirm that a normal response is returned. Once that passes, test multi-turn conversations and code modification tasks, and confirm that reasoning content is displayed correctly.

    If the problem persists, proceed in the following order:

    • Restore the configuration file from the backup taken before the changes and check whether the error disappears, to determine whether the issue was caused by the changes.
    • Keep the logs collected in Step 1 and compare them together with the checklist results.
    • When consulting platform documentation or contacting platform technical support, provide the model ID, Base URL, error text, and redacted message order. Do not provide raw keys.

    Following this process, most message ordering issues can be located in a single troubleshooting pass. The key is to change only one item at a time and always base the diagnosis on the message order in the request body.