switchyard-translation
Pure Rust translation between OpenAI Chat Completions, OpenAI Responses, and Anthropic Messages request, response, and streaming formats.
The crate translates through provider-neutral LLM types from switchyard-protocol and does not
depend on provider SDKs, HTTP servers, Python, or FFI bindings.
This is a translation tool, not a validation tool. It assumes each request or response conforms to its source API’s schema. Unless explicitly documented, the library makes no guarantees about how malformed payloads are handled.
Translate a request
use json;
use ;
Translate a response
use json;
use ;
To restore Responses tool namespaces and custom tool calls, use
translate_response_with_extensions(source, target, body, request_extensions, policy).
Pass &decoded_request.request.extensions from the original request returned by
decode_request. Empty extensions preserve the behavior of translate_response.
Extensions apply after encoding, including when a same-format response is preserved.
Translation policy
The examples above use TranslationPolicy::default(). A translation policy decides what happens
when an item cannot be translated exactly: keep it, warn about it, or return an error.
The default policy:
- Allows known information loss with warnings.
- Generates stable IDs when the source has none.
- Keeps the source payload in memory.
- Leaves target capabilities unspecified.
The lossy_conversion_policy field controls whether known conversions that lose
information are allowed. LossyConversionPolicy has two variants:
AllowWithDiagnostics(default): continue translation and record alossy_conversionwarning in the result'sdiagnosticsfield.Reject: returnTranslationError::LossyConversionwhen a known lossy conversion is encountered.
Calling translate_request/translate_response annotates every diagnostic with
source and target formats.
To reject known lossy conversions:
use ;
let policy = TranslationPolicy ;
License
Licensed under the Apache License, Version 2.0. See the Switchyard repository for details.