Skip to content

Tools, tool_choice and non-text content are dropped in provider conversion #50

Closed

Description

@Android-Login-Analysis

Summary

Six constructs do not survive conversion, and they share two causes: the provider request types have no field for tools, and the Gemini part type can only hold text.

# route caller sent forwarded
1 OpenAI to Anthropic tools and named tool_choice {"role":"user","content":"..."} only
2 OpenAI to Gemini tools and named tool_choice {"role":"user","parts":[{"text":"..."}]} only
3 OpenAI to Gemini image_url with a data URL the whole content array serialised into a text part
4 OpenAI to Gemini role: tool message promoted to role: user with the text
5 Anthropic to OpenAI image block the Anthropic block forwarded unchanged
6 OpenAI to Anthropic image_url part the OpenAI part forwarded unchanged

Case 3 is the most visible. The content array is stringified and sent as prose:

"parts": [{"text": "[{\"image_url\":{\"url\":\"data:image/png;base64,iVBOR...\"},\"type\":\"image_url\"}]"}]

The model receives a JSON string describing an image instead of an image.

Cases 1 and 2 have the same cause. Neither provider request type has a tools field:

struct GeminiRequest {
    contents: Vec<GeminiContent>,
    system_instruction: Option<GeminiContent>,
    generation_config: Option<GeminiGenerationConfig>,
}

crates/tw-provider/src/providers/google.rs:29. AnthropicRequest in crates/tw-provider/src/providers/anthropic.rs likewise has no tools or tool_choice member.
The OpenAI top-level fields do survive deserialization in ChatCompletionRequest.extra, so the values reach the converter and have nowhere to go.

Case 3 and 4 follow from the part type. crates/tw-provider/src/providers/google.rs:45:

struct GeminiPart {
    #[serde(skip_serializing_if = "Option::is_none")]
    text: Option<String>,
}

A part can only hold text, and convert_request at line 83 makes the consequence explicit:

let text = match &msg.content {
    serde_json::Value::String(s) => s.clone(),
    other => other.to_string(),
};

Any content that is not a plain string takes the other arm, and to_string() on a serde_json::Value produces its JSON text. That is the stringification in case 3. The same absence of a non-text part is why a role: tool message becomes a user turn in case 4.

Cases 5 and 6, the converters copy msg.content across without mapping carriers, so an Anthropic image block reaches an OpenAI upstream in Anthropic shape and the reverse.

Reproduction

# case 1
curl -X POST http://<gateway>/v1/chat/completions \
  -H 'content-type: application/json' -H "authorization: Bearer $KEY" \
  -d '{"model":"<anthropic-model>","messages":[{"role":"user","content":"Use the submit tool."}],
       "tool_choice":{"type":"function","function":{"name":"submit"}},
       "tools":[{"type":"function","function":{"name":"submit","description":"t",
                 "parameters":{"type":"object","properties":{}}}}]}'

# case 3
curl -X POST http://<gateway>/v1/chat/completions \
  -H 'content-type: application/json' -H "authorization: Bearer $KEY" \
  -d '{"model":"<gemini-model>","messages":[{"role":"user","content":[
       {"type":"image_url","image_url":{"url":"data:image/png;base64,iVBORw0KGgo="}}]}]}'

Then read the request sent upstream.

Expected

Cases 1 and 2, add tools and tool_choice to the provider request types and map them. Both targets define tool calling, so the values have a destination.

Case 3, GeminiPart needs an inline_data variant so an image can be carried as bytes rather than serialised into prose. Stringifying the content array is worse than dropping it, because the model treats the JSON as user text.

Case 4, a role: tool message maps onto a Gemini functionResponse part.

Cases 5 and 6, the content carriers need translating rather than copying. An Anthropic image block and an OpenAI image_url part are not interchangeable on the wire.

Version

ThinkWatch-Core 20e5b6b, reached from ThinkWatch 7a6a93d.

Activity

  1. fylorn commented on Sep 23, 2026

    @fylorn
    Contributor

    Fixed by removing the provider conversion layer rather than patching it.

    • Server edition (feat!: forward what can be forwarded, convert only what must be ThinkWatch#26, in dev): a request whose route speaks the caller's format is forwarded as sent, with only the model name and PII placeholders changed. A request that has to cross formats goes through tw-dialect, which carries tools, tool_choice and non-text content across all five formats. An integration test now checks that an Anthropic request with its system array, tools, tool_choice, metadata and cache_control reaches the upstream byte-for-byte.
    • Core (feat(upstream)!: read Bedrock's stream, and drop the provider layer #144, v0.35.0): tw-provider and tw-protocol are deleted. What the server edition still needed from them for Bedrock (eventstream unframing, usage names) now lives in tw-upstream and tw-wire.
  2. Android-Login-Analysis commented on Sep 24, 2026

    @Android-Login-Analysis
    Author

    @fylorn Thanks for the quick fix. I rechecked against tw-dialect in ThinkWatch-Core v0.43.0 (caec54c), the version ThinkWatch ff167b3 pins. Tools, tool_choice, images and PDFs now cross formats. Four inputs from the original cases are still lost.

    1. OpenAI file part with only file_id

    OpenAI to Anthropic and OpenAI to Gemini. The Chat decoder keeps a file part only when it has file_data (crates/tw-dialect/src/chat/request.rs:214).

    2. OpenAI input_audio part

    OpenAI to Anthropic and OpenAI to Gemini. user_parts has no case for input_audio, so the part is dropped (crates/tw-dialect/src/chat/request.rs:219). Gemini accepts audio as inlineData.

    3. Anthropic container_upload block

    Anthropic to OpenAI and Anthropic to Gemini. The Anthropic decoder has no case for container_upload, so its file_id is not carried (crates/tw-dialect/src/anthropic/request.rs:186).

    4. Tool strict on the Gemini target

    Anthropic to Gemini and OpenAI to Gemini. The Gemini encoder keeps only the schema (crates/tw-dialect/src/gemini/request.rs:385). The Chat and Anthropic encoders keep strict. Unlike 1 to 3, this one is not added to the drop list.

    Please correct me from wrong if I missed anything. Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions