Agentic Chat tool_use ids without tool_result
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
Problem to solve
We see this error quite often in production: https://new-sentry.gitlab.net/organizations/gitlab/issues/1731264/?environment=production&project=36&query=is%3Aunresolved&referrer=issue-stream&statsPeriod=7d&stream_index=4
What is the current bug behavior?
Request fails hard, user is forced to retry, but subsequent retries fail. This effectively forces the user to start a new chat session.
Potential Root causes
One theory is that it is due to messages being trimmed, which may be the case.
But another potential root cause is that the Anthropic API calls occasionally will return the messages out of order with tool use and result, and we must re-order them in order for their API to see the request body as valid in the next request. This root cause seems likely because this bug has been reported by other consumers of the Anthropic API and was reportedly solved by re-ordering the messages.
More context in this issue: https://github.com/anthropics/claude-code/issues/473
And in this issue: https://github.com/langchain-ai/langgraphjs/issues/1423
Relevant logs and/or screenshots
From LangSmith: 2e5fef89-28da-4739-bd75-c4567a8c1b37
BadRequestError("Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.22: `tool_use` ids were found without `tool_result` blocks immediately after: toolu_01LH6hajbBykU3ZrpSZWrweq. Each `tool_use` block must have a corresponding `tool_result` block in the next message.'}}")Traceback (most recent call last):
File "/home/aigateway/app/venv/ai-gateway-M6hW6iiC-py3.12/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 1089, in _agenerate_with_cache
async for chunk in self._astream(messages, stop=stop, **kwargs):
File "/home/aigateway/app/venv/ai-gateway-M6hW6iiC-py3.12/lib/python3.12/site-packages/langchain_anthropic/chat_models.py", line 1442, in _astream
_handle_anthropic_bad_request(e)
File "/home/aigateway/app/venv/ai-gateway-M6hW6iiC-py3.12/lib/python3.12/site-packages/langchain_anthropic/chat_models.py", line 1422, in _astream
stream = await self._acreate(payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/aigateway/app/venv/ai-gateway-M6hW6iiC-py3.12/lib/python3.12/site-packages/langchain_anthropic/chat_models.py", line 1368, in _acreate
return await self._async_client.beta.messages.create(**payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/aigateway/app/venv/ai-gateway-M6hW6iiC-py3.12/lib/python3.12/site-packages/anthropic/resources/beta/messages/messages.py", line 2354, in create
return await self._post(
^^^^^^^^^^^^^^^^^
File "/home/aigateway/app/venv/ai-gateway-M6hW6iiC-py3.12/lib/python3.12/site-packages/anthropic/_base_client.py", line 1888, in post
return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/aigateway/app/venv/ai-gateway-M6hW6iiC-py3.12/lib/python3.12/site-packages/anthropic/_base_client.py", line 1688, in request
raise self._make_status_error_from_response(err.response) from None
anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.22: `tool_use` ids were found without `tool_result` blocks immediately after: toolu_01LH6hajbBykU3ZrpSZWrweq. Each `tool_use` block must have a corresponding `tool_result` block in the next message.'}}
Example sentry error: https://new-sentry.gitlab.net/organizations/gitlab/issues/1752604/?project=36&query=is%3Aunresolved&referrer=issue-stream&statsPeriod=14d&stream_index=7