nic-6443 opened a new pull request, #13181:
URL: https://github.com/apache/apisix/pull/13181

   ## Description
   
   Add native support for the Anthropic Messages API (`/v1/messages`) to 
`ai-proxy`, enabling clients to use Anthropic's native protocol format directly 
instead of only the OpenAI-compatible endpoint.
   
   ### What this PR does
   
   Currently, requests to the Anthropic provider are handled exclusively 
through the OpenAI-compatible endpoint (`/v1/chat/completions`). This PR adds a 
native Anthropic Messages protocol adapter so clients can send requests in 
Anthropic's native format and receive responses in the same format.
   
   **New files:**
   
   1. **`ai-protocols/anthropic-messages.lua`** — Protocol adapter for the 
Anthropic Messages API:
      - URI-based detection (`/v1/messages` suffix)
      - Streaming and non-streaming request processing
      - Native Anthropic SSE event parsing (`message_start`, 
`content_block_delta`, `message_delta`, `message_stop`, `error`)
      - Usage extraction (`input_tokens`/`output_tokens`)
      - Deny-response building (both streaming and non-streaming)
      - Message manipulation (prepend/append) for prompt decorator integration
   
   2. **`ai-protocols/converters/anthropic-messages-to-openai-chat.lua`** — 
Bidirectional converter:
      - Anthropic → OpenAI request conversion (system prompts, messages, 
`tool_use`/`tool_result`, parameter mapping like `max_tokens` → 
`max_completion_tokens`)
      - OpenAI → Anthropic response conversion (content blocks, tool calls → 
`tool_use`, `finish_reason` mapping)
      - Full streaming SSE translation between protocols (OpenAI chunks → 
Anthropic event sequence)
   
   3. **`t/plugin/ai-proxy-protocol-conversion.t`** — 26 test cases covering:
      - Basic Anthropic→OpenAI non-streaming conversion
      - Error cases (missing messages, malformed JSON, wrong types, empty array)
      - Streaming SSE conversion
      - System prompt conversion
      - Tool calling round-trip
      - Edge cases: null `finish_reason`, duplicate `finish_reason` chunks, 
`usage:null`, deferred usage flush, empty SSE frames
   
   **Modified files:**
   
   4. **`ai-protocols/init.lua`** — Register `anthropic-messages` in the 
protocol registry and detection order (before `openai-chat`, since URI+body 
matching takes precedence)
   5. **`ai-protocols/converters/init.lua`** — Register the 
`anthropic-messages-to-openai-chat` converter
   6. **`ai-providers/anthropic.lua`** — Add `anthropic-messages` capability 
(`/v1/messages` path) alongside existing `openai-chat` capability
   
   ### How it works
   
   The three-layer architecture (protocols → providers → transport) already 
supports protocol conversion. This PR plugs in the Anthropic Messages protocol:
   
   - **Anthropic client → Anthropic provider**: Passthrough (native capability, 
no conversion needed)
   - **Anthropic client → OpenAI-compatible provider**: The converter bridges 
the protocol gap automatically
   - Detection order places `anthropic-messages` before `openai-chat` because 
Anthropic detection requires both URI suffix and body format matching
   
   ### No changes needed
   
   - `ai-providers/base.lua` — Already has converter support and 
Anthropic-aware `merge_usage()`
   - `ai-proxy/base.lua` — Already has the full capability routing and 
converter bridge pipeline
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to