NeMo-Relay icon indicating copy to clipboard operation
NeMo-Relay copied to clipboard

[Bug]: A provider stream that drops before its first event reaches the client as 400 "invalid hook payload"

Open Somay-kousis opened this issue 3 weeks ago • 2 comments

Affected area

Rust core runtime

Current behavior

If the provider sends 200 headers for a streaming /v1/messages call and then closes the connection before any event, the client gets:

HTTP 400
{"error":{"message":"invalid hook payload: internal error: error decoding response body","type":"nemo_relay_gateway_error"}}

No plugin is needed. The same drop on a buffered call already returns 502 gateway upstream error: error decoding response body ..., so only the streaming path gets it wrong. In run_managed_streaming (crates/cli/src/gateway/mod.rs:1120-1126) the error isn't a captured upstream failure, so translate_runtime_error (:2100) turns it into InvalidPayload, which error.rs:270 maps to 400.

Real Claude Code 2.1.316, mock provider that drops the first N streams this way:

Drops Claude Code direct Through nemo-relay run --agent claude
3 recovers recovers
6 backs off, recovers on try 7 after 37 to 39 s 6 tries in under half a second, each gets the 400, exits 1 (3 of 3 runs)

Claude Code retries the 400 with no backoff, so through Relay it uses all 6 tries in under half a second and prints API Error: 400 invalid hook payload: internal error: error decoding response body. Against a mock that returns 502 six times, Claude Code direct backs off and recovers on try 7, same as with the drops.

Expected behavior

#1652 keeps errors before the first event as HTTP errors, and that's fine. The problem is the status. The provider sent 200, so there is no provider status to keep, and Relay makes up a 400. I'd expect the 502 the buffered path already returns, with a message that doesn't say "hook payload".

Update: I had this wrong. Will's reply below says to mimic the upstream status, and the provider sent 200. So #1743 keeps the 200 and sends the same in-band SSE error Relay already sends for a failure after the first event, not a 502.

Steps to reproduce

  1. Mock that answers every POST with 200 SSE headers and then closes:

    import socket
    from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
    
    class H(BaseHTTPRequestHandler):
        protocol_version = "HTTP/1.1"
        def do_POST(self):
            self.rfile.read(int(self.headers.get("content-length", 0)))
            self.send_response(200)
            self.send_header("content-type", "text/event-stream")
            self.send_header("transfer-encoding", "chunked")
            self.end_headers()
            self.wfile.flush()
            self.connection.shutdown(socket.SHUT_RDWR)
    
    ThreadingHTTPServer(("127.0.0.1", 4391), H).serve_forever()
    
  2. In config.toml:

    [upstream]
    anthropic_base_url = "http://127.0.0.1:4391"
    
  3. nemo-relay --bind 127.0.0.1:4040 gateway start

  4. Streaming request, gets 400:

    curl -s -w '\n%{http_code}\n' http://127.0.0.1:4040/v1/messages \
      -H 'anthropic-version: 2023-06-01' -H 'content-type: application/json' \
      -d '{"model":"claude-haiku-4-5","max_tokens":16,"stream":true,"messages":[{"role":"user","content":"hi"}]}'
    

    The same request with "stream":false gets 502.

  5. With Claude Code: ANTHROPIC_API_KEY=sk-ant-fake nemo-relay run --agent claude --anthropic-base-url http://127.0.0.1:4391 -- -p "say hi" exits 1 with the 400.

Environment

  • NeMo Relay version or commit: main at 9258c97b7
  • OS and architecture: macOS arm64
  • Rust version: 1.96.1
  • Claude Code version: 2.1.316
  • Install/build command: cargo build -p nemo-relay-cli

Not run: Codex. The Responses codec's "stream ended before its final response event" error looks like it takes the same path.

Impact

A short provider blip that Claude Code survives on its own becomes a fatal error through Relay, and the message points at hooks instead of the provider. No workaround on the Relay side.

Related: #1641, #1648.

Should this be captured as an upstream transport failure the way the buffered path does, or just map to 502 in translate_runtime_error? I'd like to work on this. Can you assign it to me?

Somay-kousis avatar Nov 12 '26 07:11 Somay-kousis

@Somay-kousis we should be mimicking upstream status codes as much as possible.

Assigned to you!

willkill07 avatar Nov 14 '26 12:11 willkill07

Please target the upstream release/0.11 branch

willkill07 avatar Nov 14 '26 12:11 willkill07