Skip to content

feat: add Anthropic Messages to OpenAI ChatCompletions translator - #464

Open
khshanovskyi wants to merge 10 commits into
developmentfrom
feat/add-openai-responses-transaltor
Open

khshanovskyi wants to merge 10 commits into
developmentfrom
feat/add-openai-responses-transaltor

Conversation

@khshanovskyi

@khshanovskyi khshanovskyi commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Applicable issues

  • fixes #

Description of changes

Add a FastAPI sub-app (aidial_adapter_bedrock/anthropic_translator/chat_completions/)
mounted at /to-chat-completions/anthropic, translating Anthropic Messages
requests (/v1/messages) into OpenAI Chat Completions API calls against
DIAL Core ({DIAL_URL}/openai/deployments/{deployment}/chat/completions)
and translating the streaming/non-streaming responses back into Anthropic
Messages shape. This lets an Anthropic-native client (e.g. Claude Code)
talk to a model that only speaks the OpenAI Chat Completions API.

  • Support the bulk of the Anthropic Messages surface: system prompt
    (string/blocks, including mid-conversation system-role turns merged
    into one leading system message), user/assistant messages, tool_result/
    tool_use, image/document blocks, tool_choice, reasoning-effort mapping
    from output_config.effort, and cache_control propagated as DIAL Core's
    custom_fields.cache_breakpoint. Unsupported features (top_k, thinking
    budget, web_search/bash/text_editor/computer/code_execution tools,
    mcp_servers, container) are dropped with a warning instead of being
    rejected, so clients keep working. There is no count_tokens endpoint,
    since Chat Completions has no token-counting API.

  • Map DIAL Core and Chat Completions errors into Anthropic-shaped error
    responses, for both the HTTP error body and the terminal SSE error
    event.

  • Promote openai from a test-only to a runtime dependency (1.107.3 ->
    2.40.0) to reuse its typed Chat Completions client and SSE handling
    for the outbound call to Core.

  • Document the new interfaces.anthropicMessages DIAL Core config
    pattern and the translator's feature-support/limitations table in
    README.md.

  • Add unit tests covering request/response conversion, the streaming
    state machine, error mapping, and the mounted endpoints.

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@khshanovskyi
khshanovskyi requested a review from adubovik as a code owner July 14, 2026 08:59
@khshanovskyi
khshanovskyi force-pushed the feat/add-openai-responses-transaltor branch from f5d9eab to d733893 Compare July 14, 2026 09:02
@khshanovskyi khshanovskyi changed the title feat: add Anthropic Messages to OpenAI Responses translator feat: add Anthropic Messages to OpenAI ChatCompletions translator Jul 23, 2026
Add a FastAPI sub-app (aidial_adapter_bedrock/anthropic_translator/chat_completions/)
mounted at /to-chat-completions/anthropic, translating Anthropic Messages
requests (/v1/messages) into OpenAI Chat Completions API calls against
DIAL Core ({DIAL_URL}/openai/deployments/{deployment}/chat/completions)
and translating the streaming/non-streaming responses back into Anthropic
Messages shape. This lets an Anthropic-native client (e.g. Claude Code)
talk to a model that only speaks the OpenAI Chat Completions API.

- Support the bulk of the Anthropic Messages surface: system prompt
  (string/blocks, including mid-conversation system-role turns merged
  into one leading system message), user/assistant messages, tool_result/
  tool_use, image/document blocks, tool_choice, reasoning-effort mapping
  from output_config.effort, and cache_control propagated as DIAL Core's
  custom_fields.cache_breakpoint. Unsupported features (top_k, thinking
  budget, web_search/bash/text_editor/computer/code_execution tools,
  mcp_servers, container) are dropped with a warning instead of being
  rejected, so clients keep working. There is no count_tokens endpoint,
  since Chat Completions has no token-counting API.
- Map DIAL Core and Chat Completions errors into Anthropic-shaped error
  responses, for both the HTTP error body and the terminal SSE error
  event.
- Promote openai from a test-only to a runtime dependency (1.107.3 ->
  2.40.0) to reuse its typed Chat Completions client and SSE handling
  for the outbound call to Core.
- Document the new interfaces.anthropicMessages DIAL Core config
  pattern and the translator's feature-support/limitations table in
  README.md.
- Add unit tests covering request/response conversion, the streaming
  state machine, error mapping, and the mounted endpoints.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@khshanovskyi
khshanovskyi force-pushed the feat/add-openai-responses-transaltor branch from 3f57ac5 to 5dcc092 Compare July 23, 2026 10:38
khshanovskyi and others added 9 commits July 23, 2026 16:49
Translate Anthropic's output_config.format (json_schema) to DIAL's
response_format on the Chat Completions request; unsupported format
types or a missing schema are dropped with a warning, consistent with
the translator's other defensive-conversion behavior.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…i-responses-transaltor

# Conflicts:
#	poetry.lock
Rolls back the capability discovery driven by the outdated
CHAT_COMPLETIONS_TRANSLATION_SPEC.md and re-implements the translator
against chatcompletion-transaltions.md, taken from the tested PoC.

DIAL Core stamps `x-dial-deployment-features` on every request it routes
here, so the `GET {DIAL_URL}/openai/models` catalog — and its per-credential
TTL/LRU cache, single-flight lock and bounded fetch — is gone, along with
the three TRANSLATOR_MODEL_CATALOG_* settings. The handler now makes exactly
one outbound call.

Only `features` is on the wire; the deployment's `defaults` and `limits` are
not, which changes three things:

- Reasoning is a single top-level `reasoning_effort` gated on
  `features.reasoning_efforts`, replacing the three mutually exclusive knobs.
  Intent is resolved first (`thinking.disabled` outranks an effort sent
  alongside it; a budget-less `thinking` block means "think at the default"),
  then degraded along none < minimal < minimum < low < medium < high < xhigh
  < max, walking down before up and never crossing `none` in either
  direction.
- Nothing is nested into a deployment's own `custom_fields.configuration`.
- `max_tokens` is forwarded verbatim: there is no ceiling to clamp against.

Also brings the rest of the translator in line with the PoC:

- `cache_control.ttl` becomes DIAL's absolute `expire_at`, with the longest
  winning where several blocks collapse onto one message or tool.
- Cache writes are recovered from `prompt_tokens_details.cache_write_tokens`
  (or `cacheWriteTokens`) and subtracted from `input_tokens` alongside cache
  reads; `reasoning_tokens` surfaces as `output_tokens_details
  .thinking_tokens` in both modes — the streaming path was dropping the
  field `MessageDeltaUsage` declares for it.
- System-origin text merges in client order rather than grouped by kind, and
  an unsupported system content block warns on the way out.
- `context_management` is warn-dropped; a stage named "thought" is
  recognised as reasoning.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Review feedback: a function returning a tuple is hard to read at the call
site, and the package carried comments that only restated the code.

`_handle_messages` opened on `base_url, req, deployment = await prepare(...)`,
which said nothing about what any of the three were. Every multi-value return
in the translator is now a single one, and the handler names and types each
step:

- `prepare` is deleted — it only bundled `require_base_url`, `parse_request`
  and `resolve_model`, which the handler now calls directly.
  `resolve_model` is renamed `resolve_deployment` to match what it returns.
- `to_chat_completions_request` takes the `ToolNameAliases` registry as a
  parameter instead of returning it, so its lifetime is visible where the
  response path reads it.
- `_convert_message` returned four values that were all independently
  derivable; `from_chat_completions` derives each one and `_content_blocks`
  returns only the blocks.
- `_convert_tool_choice` is split from `_convert_parallel_tool_calls`:
  `disable_parallel_tool_use` is read independently of `tool_choice.type`, so
  the split states that rather than hiding it in a tuple.
- `_system_text`/`_collect_system` return a `SystemPrompt`, and
  `apply_stop_sequences` a `StopMatch`.
- `_tool_result_output` is split into `_tool_result_message` and
  `_tool_result_images`.
- `caller_credential` is inlined into `core_headers`, its only caller since
  the model catalog was removed.

`emulated_stop_sequences` replaces the inline conditional in the handler and
sits beside `strips_stop_parameter`, which decides the mirror case on the
request side. `_blocks` now filters non-object entries, so every content-array
walk is defensive rather than just the tool-result one.

Comments are cut by roughly 40%: anything narrating the next statement is
gone, while the decisions and incidents the code cannot show — the Vertex
`thinking_level` rejection, `strict=False` against `strict=True`, the
`SdkTool | StaticTool` annotation, the bool-is-an-int budget trap — stay.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
# Conflicts:
#	.ort.yml
#	README.md
#	aidial_adapter_bedrock/app.py
#	poetry.lock
#	pyproject.toml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant