llmshim 0.1.23

Blazing fast LLM API translation layer in pure Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/// Integration tests for vision/image support across all providers.
/// Run with: cargo test --test integration_vision -- --ignored
use serde_json::{json, Value};

fn router() -> llmshim::router::Router {
    llmshim::router::Router::from_env()
}

/// A tiny 1x1 yellow PNG pixel as base64.
const TINY_PNG_B64: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==";

fn extract_content(resp: &Value) -> String {
    resp["choices"][0]["message"]["content"]
        .as_str()
        .unwrap_or("")
        .to_lowercase()
}

fn openai_format_image() -> Value {
    json!({"type": "image_url", "image_url": {"url": format!("data:image/png;base64,{}", TINY_PNG_B64)}})
}

fn anthropic_format_image() -> Value {
    json!({"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": TINY_PNG_B64}})
}

fn make_vision_msg(image_block: Value) -> Value {
    json!({
        "role": "user",
        "content": [
            {"type": "text", "text": "Describe this image in one short sentence."},
            image_block
        ]
    })
}

// ============================================================
// Basic vision — each provider, each format
// ============================================================

#[tokio::test]
#[ignore]
async fn anthropic_vision_openai_format() {
    if std::env::var("ANTHROPIC_API_KEY").is_err() {
        return;
    }
    let router = router();
    let req = json!({
        "model": "anthropic/claude-sonnet-4-6",
        "messages": [make_vision_msg(openai_format_image())],
        "max_tokens": 100,
    });
    let resp = llmshim::completion(&router, &req).await.unwrap();
    let content = extract_content(&resp);
    println!("Anthropic (OpenAI format): {}", content);
    assert!(!content.is_empty(), "Expected content");
}

#[tokio::test]
#[ignore]
async fn anthropic_vision_native_format() {
    if std::env::var("ANTHROPIC_API_KEY").is_err() {
        return;
    }
    let router = router();
    let req = json!({
        "model": "anthropic/claude-sonnet-4-6",
        "messages": [make_vision_msg(anthropic_format_image())],
        "max_tokens": 100,
    });
    let resp = llmshim::completion(&router, &req).await.unwrap();
    let content = extract_content(&resp);
    println!("Anthropic (native format): {}", content);
    assert!(!content.is_empty(), "Expected content");
}

#[tokio::test]
#[ignore]
async fn openai_vision_anthropic_format() {
    if std::env::var("OPENAI_API_KEY").is_err() {
        return;
    }
    let router = router();
    let req = json!({
        "model": "openai/gpt-5.4",
        "messages": [make_vision_msg(anthropic_format_image())],
        "max_tokens": 200,
    });
    let resp = llmshim::completion(&router, &req).await.unwrap();
    let content = extract_content(&resp);
    println!("OpenAI (Anthropic format): {}", content);
    assert!(!content.is_empty(), "Expected content");
}

#[tokio::test]
#[ignore]
async fn gemini_vision_openai_format() {
    if std::env::var("GEMINI_API_KEY").is_err() {
        return;
    }
    let router = router();
    let req = json!({
        "model": "gemini/gemini-3-flash-preview",
        "messages": [make_vision_msg(openai_format_image())],
        "max_tokens": 200,
    });
    let resp = llmshim::completion(&router, &req).await.unwrap();
    let content = extract_content(&resp);
    println!("Gemini (OpenAI format): {}", content);
    assert!(!content.is_empty(), "Expected content");
}

#[tokio::test]
#[ignore]
async fn gemini_vision_anthropic_format() {
    if std::env::var("GEMINI_API_KEY").is_err() {
        return;
    }
    let router = router();
    let req = json!({
        "model": "gemini/gemini-3-flash-preview",
        "messages": [make_vision_msg(anthropic_format_image())],
        "max_tokens": 200,
    });
    let resp = llmshim::completion(&router, &req).await.unwrap();
    let content = extract_content(&resp);
    println!("Gemini (Anthropic format): {}", content);
    assert!(!content.is_empty(), "Expected content");
}

// ============================================================
// Vision with reasoning/thinking
// ============================================================

#[tokio::test]
#[ignore]
async fn anthropic_vision_with_thinking() {
    if std::env::var("ANTHROPIC_API_KEY").is_err() {
        return;
    }
    let router = router();
    let req = json!({
        "model": "anthropic/claude-sonnet-4-6",
        "messages": [make_vision_msg(openai_format_image())],
        "max_tokens": 4000,
        "thinking": {"type": "enabled", "budget_tokens": 2000},
    });
    let resp = llmshim::completion(&router, &req).await.unwrap();
    let content = extract_content(&resp);
    let reasoning = resp["choices"][0]["message"].get("reasoning_content");
    println!("Vision+thinking content: {}", content);
    println!(
        "Vision+thinking reasoning: {}",
        reasoning.and_then(|r| r.as_str()).unwrap_or("none")
    );
    assert!(!content.is_empty(), "Expected content");
    assert!(
        reasoning.is_some(),
        "Expected reasoning with thinking enabled"
    );
}

// ============================================================
// Interleaved models with image in conversation
// ============================================================

#[tokio::test]
#[ignore]
async fn vision_interleaved_anthropic_then_gemini() {
    if std::env::var("ANTHROPIC_API_KEY").is_err() || std::env::var("GEMINI_API_KEY").is_err() {
        return;
    }
    let router = router();

    // Turn 1: Anthropic describes the image
    let req1 = json!({
        "model": "anthropic/claude-sonnet-4-6",
        "messages": [make_vision_msg(openai_format_image())],
        "max_tokens": 200,
    });
    let resp1 = llmshim::completion(&router, &req1).await.unwrap();
    let msg1 = resp1["choices"][0]["message"].clone();
    let content1 = extract_content(&resp1);
    println!("Turn 1 (Anthropic): {}", content1);
    assert!(!content1.is_empty());

    // Turn 2: Gemini continues (text-only follow-up)
    let req2 = json!({
        "model": "gemini/gemini-3-flash-preview",
        "messages": [
            make_vision_msg(openai_format_image()),
            msg1,
            {"role": "user", "content": "Based on your description, what mood does this image convey? One word."},
        ],
        "max_tokens": 200,
    });
    let resp2 = llmshim::completion(&router, &req2).await.unwrap();
    let content2 = extract_content(&resp2);
    println!("Turn 2 (Gemini): {}", content2);
    assert!(!content2.is_empty());
}

#[tokio::test]
#[ignore]
async fn vision_interleaved_gemini_then_openai() {
    if std::env::var("GEMINI_API_KEY").is_err() || std::env::var("OPENAI_API_KEY").is_err() {
        return;
    }
    let router = router();

    // Turn 1: Gemini describes the image
    let req1 = json!({
        "model": "gemini/gemini-3-flash-preview",
        "messages": [make_vision_msg(anthropic_format_image())],
        "max_tokens": 200,
    });
    let resp1 = llmshim::completion(&router, &req1).await.unwrap();
    let msg1 = resp1["choices"][0]["message"].clone();
    let content1 = extract_content(&resp1);
    println!("Turn 1 (Gemini): {}", content1);
    assert!(!content1.is_empty());

    // Turn 2: OpenAI follows up
    let req2 = json!({
        "model": "openai/gpt-5.4",
        "messages": [
            {"role": "user", "content": "I showed you an image earlier and you said something about it."},
            msg1,
            {"role": "user", "content": "What else might you say about a tiny 1-pixel image?"},
        ],
        "max_tokens": 200,
    });
    let resp2 = llmshim::completion(&router, &req2).await.unwrap();
    let content2 = extract_content(&resp2);
    println!("Turn 2 (OpenAI): {}", content2);
    assert!(!content2.is_empty());
}

// ============================================================
// Three-provider hop with image
// ============================================================

#[tokio::test]
#[ignore]
async fn vision_three_provider_hop() {
    if std::env::var("ANTHROPIC_API_KEY").is_err()
        || std::env::var("OPENAI_API_KEY").is_err()
        || std::env::var("GEMINI_API_KEY").is_err()
    {
        return;
    }
    let router = router();

    // Turn 1: Anthropic sees image
    let req1 = json!({
        "model": "anthropic/claude-sonnet-4-6",
        "messages": [make_vision_msg(openai_format_image())],
        "max_tokens": 200,
    });
    let resp1 = llmshim::completion(&router, &req1).await.unwrap();
    let msg1 = resp1["choices"][0]["message"].clone();
    println!("Turn 1 (Anthropic): {}", extract_content(&resp1));

    // Turn 2: Gemini continues
    let req2 = json!({
        "model": "gemini/gemini-3-flash-preview",
        "messages": [
            make_vision_msg(openai_format_image()),
            msg1,
            {"role": "user", "content": "Summarize what you see in 3 words."},
        ],
        "max_tokens": 200,
    });
    let resp2 = llmshim::completion(&router, &req2).await.unwrap();
    let msg2 = resp2["choices"][0]["message"].clone();
    println!("Turn 2 (Gemini): {}", extract_content(&resp2));

    // Turn 3: OpenAI wraps up
    let req3 = json!({
        "model": "openai/gpt-5.4",
        "messages": [
            {"role": "user", "content": "We've been discussing a tiny image."},
            msg1,
            {"role": "user", "content": "Another model also saw it."},
            msg2,
            {"role": "user", "content": "Do you agree with both descriptions? Yes or no."},
        ],
        "max_tokens": 200,
    });
    let resp3 = llmshim::completion(&router, &req3).await.unwrap();
    let content3 = extract_content(&resp3);
    println!("Turn 3 (OpenAI): {}", content3);
    assert!(!content3.is_empty());
}

// ============================================================
// Interleaved text + images — position matters
// ============================================================

#[tokio::test]
#[ignore]
async fn anthropic_interleaved_text_image_position() {
    if std::env::var("ANTHROPIC_API_KEY").is_err() {
        return;
    }
    let router = router();

    // Send two identical images with different labels — model should distinguish by position
    let req = json!({
        "model": "anthropic/claude-sonnet-4-6",
        "messages": [{
            "role": "user",
            "content": [
                {"type": "text", "text": "I'll show you two images labeled A and B."},
                {"type": "text", "text": "Image A:"},
                {"type": "image_url", "image_url": {"url": format!("data:image/png;base64,{}", TINY_PNG_B64)}},
                {"type": "text", "text": "Image B:"},
                {"type": "image_url", "image_url": {"url": format!("data:image/png;base64,{}", TINY_PNG_B64)}},
                {"type": "text", "text": "How many images did I show you? Reply with just the number."}
            ]
        }],
        "max_tokens": 100,
    });
    let resp = llmshim::completion(&router, &req).await.unwrap();
    let content = extract_content(&resp);
    println!("Interleaved test: {}", content);
    assert!(
        content.contains('2'),
        "Expected 2 images recognized, got: {}",
        content
    );
}

#[tokio::test]
#[ignore]
async fn gemini_interleaved_text_image_position() {
    if std::env::var("GEMINI_API_KEY").is_err() {
        return;
    }
    let router = router();

    let req = json!({
        "model": "gemini/gemini-3-flash-preview",
        "messages": [{
            "role": "user",
            "content": [
                {"type": "text", "text": "Image A:"},
                {"type": "image_url", "image_url": {"url": format!("data:image/png;base64,{}", TINY_PNG_B64)}},
                {"type": "text", "text": "Image B:"},
                {"type": "image_url", "image_url": {"url": format!("data:image/png;base64,{}", TINY_PNG_B64)}},
                {"type": "text", "text": "How many images did I show you? Reply with just the number."}
            ]
        }],
        "max_tokens": 200,
    });
    let resp = llmshim::completion(&router, &req).await.unwrap();
    let content = extract_content(&resp);
    println!("Gemini interleaved: {}", content);
    assert!(content.contains('2'), "Expected 2 images, got: {}", content);
}

#[tokio::test]
#[ignore]
async fn openai_interleaved_text_image_position() {
    if std::env::var("OPENAI_API_KEY").is_err() {
        return;
    }
    let router = router();

    let req = json!({
        "model": "openai/gpt-5.4",
        "messages": [{
            "role": "user",
            "content": [
                {"type": "text", "text": "I'll show you two images labeled A and B."},
                {"type": "text", "text": "Image A:"},
                {"type": "image_url", "image_url": {"url": format!("data:image/png;base64,{}", TINY_PNG_B64)}},
                {"type": "text", "text": "Image B:"},
                {"type": "image_url", "image_url": {"url": format!("data:image/png;base64,{}", TINY_PNG_B64)}},
                {"type": "text", "text": "How many images did I show you? Reply with just the number."}
            ]
        }],
        "max_tokens": 200,
    });
    let resp = llmshim::completion(&router, &req).await.unwrap();
    let content = extract_content(&resp);
    println!("OpenAI interleaved: {}", content);
    assert!(
        content.contains('2'),
        "Expected 2 images recognized, got: {}",
        content
    );
}

/// xAI Grok models do NOT support vision — verify we get a clear error.
#[tokio::test]
#[ignore]
async fn xai_vision_returns_error() {
    if std::env::var("XAI_API_KEY").is_err() {
        return;
    }
    let router = router();

    let req = json!({
        "model": "xai/grok-4-1-fast-non-reasoning",
        "messages": [{
            "role": "user",
            "content": [
                {"type": "text", "text": "Describe this"},
                {"type": "image_url", "image_url": {"url": format!("data:image/png;base64,{}", TINY_PNG_B64)}}
            ]
        }],
        "max_tokens": 200,
    });
    let result = llmshim::completion(&router, &req).await;
    assert!(result.is_err(), "xAI should reject vision requests");
    println!("xAI vision error (expected): {}", result.unwrap_err());
}