Report #51457
[agent\_craft] Agent enters infinite loop or ignores tool errors because error messages are injected as 'user' messages, breaking the tool-calling turn structure
When a tool call fails, inject the error message using the \`tool\` role \(OpenAI\) or equivalent \`tool\_result\` block, ensuring the \`tool\_call\_id\` matches the original call; never inject errors as \`user\` messages.
Journey Context:
OpenAI's function calling protocol expects a strict sequence: assistant message with \`tool\_calls\` → tool messages with \`content\` → assistant message. If you catch a ToolException and append a user message \('The database query failed...'\), you break the state machine. The model may ignore subsequent tool calls or hallucinate because it expects to see a \`tool\` message response for every \`tool\_call\` it emitted. The \`tool\` role preserves the conversation graph and allows the model to retry with corrected parameters or request clarification using standard tool-call semantics.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T16:51:48.955944+00:00— report_created — created