Skip to content

Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.

Rust

fix(translation): number Chat tool calls within tool_calls instead of by content index (#712)

Lin Jia45469b7377fc
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ed16e6d1e..b7429ffd1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -128,6 +128,13 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
Completions, Anthropic Messages, and Responses return HTTP 502. Error replies
preserve the provider's message and, for OpenAI applications, a nonempty
string error code. Switchyard removes echoed caller credentials.
+- **Chat tool-call index counts tool calls, not content blocks** — the OpenAI
+ Chat stream encoder copied the source index into `tool_calls[].index`.
+ Anthropic and Responses index the whole content array, so text ahead of the
+ first tool call pushed the sole call to index 1, and the official OpenAI SDK,
+ which subscripts its `tool_calls` array with that index, raised `IndexError`.
+ Tool calls are now numbered within the Chat `tool_calls` array in order of
+ first appearance.
- **Advisor stall checkpoint re-arms after a refunded review** — a
stall-triggered consult that failed open or returned an unparseable verdict
refunded the review budget but left the conversation's stall latch set, so
diff --git a/crates/switchyard-translation/src/codecs/openai_chat/stream.rs b/crates/switchyard-translation/src/codecs/openai_chat/stream.rs
index e640d196b..e87e21728 100644
--- a/crates/switchyard-translation/src/codecs/openai_chat/stream.rs
+++ b/crates/switchyard-translation/src/codecs/openai_chat/stream.rs
@@ -238,13 +238,29 @@ fn encode_openai_chat_stream(
id,
name,
arguments_delta,
- } => vec![openai_tool_call_chunk(
- state,
- index,
- id,
- name,
- arguments_delta,
- )],
+ } => {
+ // The source index counts every content block (Anthropic) or output item
+ // (Responses), so text ahead of the first tool call shifts it. Chat clients use
+ // the index as a subscript into `tool_calls`, so number calls in that array
+ // instead, in order of first appearance.
+ let tool = state.tool_states.entry(index).or_default();
+ let chat_index = match tool.chat_tool_index {
+ Some(chat_index) => chat_index,
+ None => {
+ let chat_index = state.next_chat_tool_index;
+ state.next_chat_tool_index += 1;
+ state.tool_states.entry(index).or_default().chat_tool_index = Some(chat_index);
+ chat_index
+ }
+ };
+ vec![openai_tool_call_chunk(
+ state,
+ chat_index,
+ id,
+ name,
+ arguments_delta,
+ )]
+ }
LlmResponseChunk::Usage(usage) => {
state.usage = usage;
state.saw_backend_usage = true;
diff --git a/crates/switchyard-translation/src/codecs/stream.rs b/crates/switchyard-translation/src/codecs/stream.rs
index 49bfdd372..a0dfc6a55 100644
--- a/crates/switchyard-translation/src/codecs/stream.rs
+++ b/crates/switchyard-translation/src/codecs/stream.rs
@@ -73,6 +73,7 @@ pub struct StreamTranslationState {
pub(crate) decoded_reasoning_ids: std::collections::BTreeSet<usize>,
pub(crate) next_response_output_index: usize,
pub(crate) response_sequence_number: u64,
+ pub(crate) next_chat_tool_index: usize,
pub(crate) reasoning_block_index: Option<usize>,
pub(crate) reasoning_block_started: bool,
@@ -113,6 +114,10 @@ pub(crate) struct StreamToolState {
pub(crate) pending_arguments: String,
pub(crate) started: bool,
pub(crate) content_index: Option<usize>,
+ /// Position in the OpenAI Chat `tool_calls` array, assigned when ENCODING. Chat numbers
+ /// tool calls on their own, while Anthropic and Responses index the whole content array,
+ /// so the source index cannot be reused.
+ pub(crate) chat_tool_index: Option<usize>,
pub(crate) response_output_index: Option<usize>,
pub(crate) response_item_id: Option<String>,
}
diff --git a/crates/switchyard-translation/tests/stream_translation.rs b/crates/switchyard-translation/tests/stream_translation.rs
index 709707d8b..fe751f969 100644
--- a/crates/switchyard-translation/tests/stream_translation.rs
+++ b/crates/switchyard-translation/tests/stream_translation.rs
@@ -1421,6 +1421,67 @@ fn responses_function_call_stream_ends_with_tool_use_on_every_wire() -> TestResu
Ok(())
}
+// Chat clients subscript `tool_calls` with the streamed index, so tool calls must be numbered
+// within that array. Anthropic and Responses index the whole content array, where text ahead
+// of the first tool call pushes it to 1; the OpenAI SDK then indexes past a one-element array.
+#[test]
+fn chat_tool_call_index_counts_tool_calls_not_content_blocks() -> TestResult {
+ let engine = TranslationEngine::default();
+ let tool_indices = |events: &[Value]| -> Vec<Value> {
+ events
+ .iter()
+ .filter_map(|event| event["choices"][0]["delta"]["tool_calls"].as_array())
+ .flat_map(|calls| calls.iter().map(|call| call["index"].clone()))
+ .collect()
+ };
+
+ // Anthropic: text at content index 0, then two tool calls at 1 and 2.
+ let tool_block = |index: usize, id: &str| {
+ vec![
+ json!({"type": "content_block_start", "index": index, "content_block": {"type": "tool_use", "id": id, "name": "lookup", "input": {}}}),
+ json!({"type": "content_block_delta", "index": index, "delta": {"type": "input_json_delta", "partial_json": "{\"q\":\"rust\"}"}}),
+ json!({"type": "content_block_stop", "index": index}),
+ ]
+ };
+ let mut anthropic = vec![
+ json!({"type": "message_start", "message": {"id": "msg_1", "type": "message", "role": "assistant", "model": "claude-test", "content": [], "usage": {"input_tokens": 1, "output_tokens": 1}}}),
+ json!({"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}),
+ json!({"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Checking now."}}),
+ json!({"type": "content_block_stop", "index": 0}),
+ ];
+ anthropic.extend(tool_block(1, "toolu_01"));
+ anthropic.extend(tool_block(2, "toolu_02"));
+ anthropic.push(json!({"type": "message_delta", "delta": {"stop_reason": "tool_use"}, "usage": {"output_tokens": 8}}));
+ anthropic.push(json!({"type": "message_stop"}));
+ let chat = translate_stream(
+ &engine,
+ WireFormat::AnthropicMessages,
+ WireFormat::OpenAiChat,
+ &anthropic,
+ )?;
+ assert_eq!(
+ tool_indices(&chat),
+ vec![json!(0), json!(0), json!(1), json!(1)]
+ );
+
+ // Responses: a message item at output index 0, then a function call at 1.
+ let responses = [
+ json!({"type": "response.created", "response": {"id": "resp_1", "model": "gpt-5.6"}}),
+ json!({"type": "response.output_text.delta", "output_index": 0, "delta": "Checking now."}),
+ json!({"type": "response.output_item.added", "output_index": 1, "item": {"id": "fc_1", "type": "function_call", "call_id": "call_1", "name": "lookup", "arguments": ""}}),
+ json!({"type": "response.function_call_arguments.delta", "output_index": 1, "delta": "{\"q\":\"rust\"}"}),
+ json!({"type": "response.completed", "response": {}}),
+ ];
+ let chat = translate_stream(
+ &engine,
+ WireFormat::OpenAiResponses,
+ WireFormat::OpenAiChat,
+ &responses,
+ )?;
+ assert_eq!(tool_indices(&chat), vec![json!(0), json!(0)]);
+ Ok(())
+}
+
// An OpenAI-shaped error frame carries no `choices`, so it must decode to a stream error
// instead of a bare message start that silently drops the upstream message.
#[test]