AI Gateway is incorrectly considering a streaming `refusal` on the Anthropic API to be successful. h

AI Gateway is incorrectly considering a streaming refusal on the Anthropic API to be successful. https://docs.anthropic.com/en/docs/test-and-evaluate/strengthen-guardrails/handle-streaming-refusals#implementation-guide

Here's is a sample response:

{
  "id": "msg_01HCDu5LRGeP2o7s2xGmxyx8",
  "type": "message",
  "role": "assistant",
  "model": "claude-opus-4-1-20250805",
  "content": "",
  "stop_reason": null,
  "stop_sequence": null,
  "usage": {
    "input_tokens": 9,
    "cache_creation_input_tokens": 16203,
    "cache_read_input_tokens": 0,
    "cache_creation": {
      "ephemeral_5m_input_tokens": 16203,
      "ephemeral_1h_input_tokens": 0
    },
    "output_tokens": 0
  },
  "streamed_data": [
    {
      "nonce": "e8071956",
      "type": "message_start",
      "message": {
        "id": "msg_01HCDu5LRGeP2o7s2xGmxyx8",
        "type": "message",
        "role": "assistant",
        "model": "claude-opus-4-1-20250805",
        "content": [],
        "stop_reason": null,
        "stop_sequence": null,
        "usage": {
          "input_tokens": 9,
          "cache_creation_input_tokens": 16203,
          "cache_read_input_tokens": 0,
          "cache_creation": {
            "ephemeral_5m_input_tokens": 16203,
            "ephemeral_1h_input_tokens": 0
          },
          "output_tokens": 0
        }
      }
    },
    {
      "type": "message_delta",
      "delta": {
        "stop_reason": "refusal",
        "stop_sequence": null
      },
      "usage": {
        "input_tokens": 9,
        "cache_creation_input_tokens": 16203,
        "cache_read_input_tokens": 0,
        "output_tokens": 0
      }
    },
    {
      "nonce": "8aeda459",
      "type": "message_stop"
    }
  ]
}
image.png
Was this page helpful?