Hi everyone! I’m just getting started with Openhan...
# questions
s
Hi everyone! I’m just getting started with Openhands and I ran into this problem when running it with Devstral-Small-2-24B-Instruct-2512 in headless mode:
"Unexpected role 'user' after role 'tool'","type":"invalid_request_error"
I feel like I might have missed a configuration somewhere. This happens intermittently, but I would say over 50% of the time.
m
Hey @straight-napkin-27920 thanks for the message. Could you share the conversation trajectory? (if it is not private)
s
There are 83 events in the conversation. Here is 82 and 83. I suppose I can dump the entire conversation here if you want, but I would probably need to do some obfuscating. Let me know if you need anything else and thanks for the help!
Copy code
{
  "id": "8fdd339d-9281-4da1-94d3-693e85c6d9ce",
  "timestamp": "2026-04-16T15:26:20.421724",
  "source": "environment",
  "forgotten_event_ids": [
    "2f517db2-8b2d-4e2c-893c-2f88f2dde95c",
    "fc7db3e7-4a5f-47df-821f-ce5d3bc6e126",
    "8b12b13d-d6e5-4f9a-86ea-75601b92b254",
    "6329ee79-c7aa-4b59-804b-714a66014a03",
    "0b3b9144-155b-43ae-8882-6cec17d124ff",
    "ea6ab82e-9100-4d9e-9a3a-cb8cd5f9e76c",
    "a73d0575-9169-469a-a7ff-b94980134f3a",
    "57901f9a-3887-45ee-934f-ee28c9389d0a",
    "b188772a-7169-43e4-bb8b-482e71c10f57",
    "fe0742c9-b167-40a2-b506-9b09e2164a77",
    "fb375a2f-10fa-441d-b4e9-d70841544166",
    "4f50c5ad-1825-4a35-b58b-4a08a227c181",
    "ac610102-8629-42c4-842c-6cd88df9c848",
    "5976f686-2d0e-4e89-92f3-9f3903a75068",
    "f2b2e3ec-3f26-4d02-821e-f7af25689ce8",
    "d8876c49-ec0b-4838-83ff-99e36af30eb7",
    "d6f45d5d-2897-4dad-8b0c-13468ac07842",
    "630b1fd9-6858-44e9-87c1-77f451229802",
    "a3962634-6937-4fb2-b78a-c76ade245299",
    "065f5260-861d-474c-951e-0cc758cfd895",
    "7522cb28-a74d-4e86-97d6-b47543847a03",
    "3f766ee6-dbba-470a-b32d-5ae0d87b9028",
    "731614b3-5871-45f2-b5c6-c42e15946947",
    "e4af7259-ada0-459d-9534-6131a98cf6a4",
    "5c1d2f27-1e74-445f-a200-90f808c848fb",
    "c6c9c4e1-ebe9-45db-88e5-5b75e807ef10",
    "784a8c09-be7f-4a7a-9047-3da39b3f2cca",
    "ddcf1ed4-519e-43cb-949a-3008f3f1ee61",
    "dc3ee4bb-75ef-4483-beba-c3c37f542382",
    "4bd984a9-8313-4f69-9876-3859ad08160d",
    "19a574b9-fb30-4910-ad0c-6ce7b8120528",
    "72d8f73e-ecb3-4737-a1b4-c553918d3a00",
    "143a734a-96f8-4603-8e2a-0f2fe20d77cd",
    "34833e30-8414-4c04-af7d-31ec989c27ce",
    "ece68066-096d-4604-ba65-618c9c957838",
    "51845670-98e6-4ec6-b94f-c9c2a6664bf9",
    "cea37701-d800-488b-9af1-60cf1261aad8",
    "5493289a-b4a5-458a-8cbd-0a51827c7f24",
    "c4a4f795-d01c-44dc-80b4-549a6157637d",
    "bd8e987b-702f-4f15-a7cf-47bdf45c40f9",
    "39947739-5ce7-4b9e-aa05-363fb3cfe9d3",
    "b3219679-2340-4201-bb51-989d02f3dc14",
    "5722a22b-6748-4a07-b8ba-b0b3daa39fcc",
    "3ae0610d-c43f-4f97-a1e7-3ed2dd77775e"
  ],
  "summary": "Here's the context-aware state summary based on the events:\n\n```\nUSER_CONTEXT: Understanding the difference between Database CRD and Cluster CRD in CNPG, and examining current database configurations in the repository\n\nTASK_TRACKING:\n- TASK-001: Research Database vs Cluster CRD differences (IN_PROGRESS)\n- TASK-002: Examine current database configurations (IN_PROGRESS)\n\nCOMPLETED:\n- Reviewed CNPG documentation for API reference\n- Examined Database CRD structure\n- Examined Cluster CRD structure\n- Found and reviewed existing database configurations in kustomize/overlays/denodo/resources/\n- Found and reviewed base database configurations in denodo/base/postgresql/\n- Found and reviewed cluster configurations (postgres-cluster.yaml and denodo-postgresql-cluster.yaml)\n\nPENDING:\n- Complete analysis of Database vs Cluster CRD differences\n- Understand how databases are currently configured and managed\n- Determine if any changes are needed to the current setup\n\nCURRENT_STATE:\n- Found 3 database YAML files in overlays/denodo/resources/:\n  - scheduler-database.yaml\n  - solutionmanager-database.yaml\n  - datacatalog-database.yaml\n- Found 4 database YAML files in base/postgresql/:\n  - denodo-vdp-database.yaml\n  - denodo-scheduler-database.yaml\n  - denodo-datacatalog-database.yaml\n  - denodo-postgresql-cluster.yaml (Cluster CRD)\n- All databases reference Cluster CRDs (postgres-cluster or denodo-postgresql-cluster)\n- Cluster configurations found in:\n  - kustomize/overlays/denodo/resources/postgres-cluster.yaml\n  - denodo/base/postgresql/denodo-postgresql-cluster.yaml\n\nCODE_STATE:\n- Database CRD files: <http://postgresql.cnpg.io/v1|postgresql.cnpg.io/v1>, kind: Database\n- Cluster CRD files: <http://postgresql.cnpg.io/v1|postgresql.cnpg.io/v1>, kind: Cluster\n- Files examined:\n  - /home/sandbox/denodo/kustomize/overlays/denodo/resources/*.yaml\n  - /home/sandbox/denodo/denodo/base/postgresql/*.yaml\n\nTESTS:\n- Found test file: /home/sandbox/denodo/tests/acceptance/test_odbc_login.py\n- No specific test failures identified yet\n\nCHANGES:\n- No changes made yet, currently in research phase\n\nDEPS:\n- CNPG API (<http://postgresql.cnpg.io/v1|postgresql.cnpg.io/v1>)\n- Kubernetes resources\n- Kustomize configurations\n\nVERSION_CONTROL_STATUS:\n- No version control operations performed yet\n- Current working directory: /home/sandbox/denodo\n```",
  "summary_offset": 4,
  "llm_response_id": "chatcmpl-c6fb8f70-0f76-49a2-b3d8-dee0fc41014a",
  "kind": "Condensation"
}
Copy code
{
  "id": "e9cf6e39-72c3-40e6-8337-213eeec77041",
  "timestamp": "2026-04-16T15:26:20.634794",
  "source": "environment",
  "code": "LLMBadRequestError",
  "detail": "litellm.BadRequestError: OpenAIException - Unexpected role 'user' after role 'tool'",
  "kind": "ConversationErrorEvent"
}
m
Hey Luke! thanks to sharing the events. This is a known issue affecting Mistral/Devstral models with OpenHands and other agentic tools. This happens because Mistral enforces strict message role ordering, it requires an assistant message between tool results and subsequent user messages. When OpenHands inserts a
Condensation
event (the context summarization you see in event 82) as a
user
role message after tool results, Mistral rejects it. Actually, it is a well-known problem: • OpenCode: https://github.com/anomalyco/opencode/issues/7488 • Zed Editor: https://github.com/zed-industries/zed/issues/37021, https://github.com/zed-industries/zed/issues/31491 • avante.nvim: https://github.com/yetone/avante.nvim/issues/2562 • OpenHands: https://github.com/All-Hands-AI/OpenHands/issues/8685 However, it seems that this was happening in OpenHands only for versions below 1. Which version are you using? (I assume greater than 1) The only workaround I have in mind right now is to use another model. @calm-artist-7206 probably you know a bit more about the problem and/or have better workarounds/solutions @wide-doctor-37830 tagging you because of the condenser
s
I think this issue has been discussed in this bug and this pull request. There was a suggestion to use the environment variable
AGENT_ENABLE_FINISH=false
. I have used this and not been able to get passed this error.
m
Actually, there is also an open issue at LiteLLM to fix that: https://github.com/BerriAI/litellm/issues/18685 but for now there are no successful solutions. You can also try to disable the condenser.
c
Or try to set
keep_first=2
condenser option, though I thought it's default
I think it shouldn't be the case that the summary ends up after a
tool
role... unless there's a bug in the condenser or
keep_first
attribute is set higher (so it will try to keep some tool calls before summary)
s
If I understand this correctly, this is resolved in this pull request although I do not see this issue in the change logs for any release - not even their nightly release.
m
I gave also a deeper look into that, and it seems it was released. Timeline - 2026-04-02 22:16 UTC — PR #24996 merged into internal dev branch litellm_internal_dev_04_02_2026 (not main directly) - 2026-04-02 23:38 UTC — Internal dev branch promoted to main via PR #25036 (commit 3604b600d3 "[Infra] Merge internal dev branch with main") - 2026-04-03 — First nightly with the fix: v1.83.1-nightly - 2026-04-14 — First stable with the fix: v1.83.3-stable All released tags containing the fix v1.83.1-nightly 2026-04-03 ← first v1.83.2-nightly 2026-04-08 v1.83.3.rc.1 2026-04-08 v1.83.5-nightly 2026-04-08 v1.83.6-nightly 2026-04-10 v1.83.7.rc.1 2026-04-12 v1.83.3-stable 2026-04-14 ← first stable v1.83.8-nightly 2026-04-15 v1.83.9-nightly 2026-04-17 ← latest The PR's own merge commit shows up in no tag because LiteLLM's workflow merges PRs into a dated internal dev branch first, then squash/merges that dev branch into main via a separate infra PR. The original merge commit lives only on the (since-deleted) dev branch; the code itself was re-merged to main under a new commit. I verified the post-PR logic (insert_before_indexes, updated docstring) is present in the current main copy of litellm/litellm_core_utils/prompt_templates/common_utils.py. 🫣 Now our sdk is using version v1.17.0. So we don't have the fix yet. This should come with the next release.
s
Thanks for looking into this! So we should expect LiteLLM in the SDK to get bumped to 1.83.x as soon as this is released? Let me know if there is anything I can help with.
m
> So we should expect LiteLLM in the SDK to get bumped to 1.83.x as soon as this is released? Yes > Let me know if there is anything I can help with. I believe we just have to wait 🙂 Thanks for sharing the issue.
👍 1