Add non-streaming responses and URL image source support

#1
by victor HF Staff - opened

What

Two improvements to the Anthropic Messages proxy.

1. Non-streaming responses

Today every request returns text/event-stream, even when the client does not ask to stream, so a normal Anthropic SDK messages.create() (non-streaming) cannot parse the response.

Now the request's stream field is respected: only stream: true returns SSE; absent or false returns a single JSON Anthropic Message (matches the Anthropic API, the SDK omits stream for non-streaming calls). For non-streaming requests the proxy asks the upstream router for a non-streaming completion (stream: false) and converts the ChatCompletion JSON to an Anthropic Message. The streaming path is unchanged.

2. URL image sources

MessageContentSource was missing a url field, so Anthropic {"type":"url","url":...} image sources were silently dropped (only base64 worked). Added the field and map both url and base64 sources to an OpenAI image_url part.

Tests

  • cargo test: unit tests for completion -> message conversion (text + tool calls) and image url/base64 conversion.
  • Verified end-to-end against the live router with the Anthropic Python SDK + curl: non-streaming text/tools, streaming text, images (url + base64, streaming + non-streaming), explicit stream:false, count_tokens, and error formatting.

Compatibility / risk

  • Behavior change: a request that omits stream (or sends false) now gets JSON instead of SSE. This is the Anthropic-spec behavior and is what makes the SDK's non-streaming path work. Clients that stream (e.g. Claude Code) send stream: true explicitly and are unaffected.
  • Rollback is a one-commit revert.
victor changed pull request status to closed

Sign up or log in to comment