genai-rs 0.8.0

A Rust client library for Google's Generative AI (Gemini) API with streaming, function calling, and multi-turn conversations
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# Function Calling Guide

This guide covers function calling fundamentals in `genai-rs`, including the `#[tool]` macro, `ToolService` for stateful functions, and manual handling patterns.

## Table of Contents

- [Overview]#overview
- [Choosing an Approach]#choosing-an-approach
- [The #[tool] Macro]#the-tool-macro
- [ToolService for Stateful Functions]#toolservice-for-stateful-functions
- [Manual Function Handling]#manual-function-handling
- [FunctionDeclaration Builder]#functiondeclaration-builder
- [Function Calling Modes]#function-calling-modes
- [Streaming Function Call Arguments]#streaming-function-call-arguments
- [Parallel and Compositional Calls]#parallel-and-compositional-calls
- [Best Practices]#best-practices

## Overview

Function calling lets the model invoke your code to get real-time data or perform actions. There are three approaches:

| Approach | Registration | State | Execution | Best For |
|----------|-------------|-------|-----------|----------|
| `#[tool]` macro | Compile-time | Stateless | Auto or manual | Simple, clean code |
| `ToolService` | Runtime | Stateful | Auto or manual | DB pools, API clients |
| Manual | Runtime | Flexible | Manual only | Custom execution logic |

## Choosing an Approach

```text
Need shared state (DB, API clients, config)?
├── Yes → Use ToolService
└── No
    ├── Simple function, want minimal code?
    │   └── Yes → Use #[tool] macro
    └── Need custom execution logic?
        └── Yes → Use manual handling
```

### Decision Matrix

| Need | Recommended Approach |
|------|---------------------|
| Quick prototype | `#[tool]` + `create_with_auto_functions()` |
| Production stateless | `#[tool]` + `create_with_auto_functions()` |
| Database access | `ToolService` |
| Per-request context | `ToolService` |
| Rate limiting | Manual handling |
| Circuit breakers | Manual handling |
| Custom logging/metrics | Manual handling |

## The #[tool] Macro

The simplest approach - define functions with the `#[tool]` attribute.

### Basic Usage

```rust,ignore
use genai_rs_macros::tool;

/// Gets the current weather for a city
#[tool(city(description = "The city to get weather for"))]
fn get_weather(city: String) -> String {
    // In production, call a weather API
    format!(r#"{{"city": "{}", "temp": "22°C"}}"#, city)
}

/// Gets current time in a timezone
#[tool(timezone(description = "Timezone like UTC, PST, EST"))]
fn get_time(timezone: String) -> String {
    format!(r#"{{"timezone": "{}", "time": "14:30"}}"#, timezone)
}
```

### Auto-Discovery and Execution

```rust,ignore
// Functions are auto-discovered from the global registry
let result = client
    .interaction()
    .with_model("gemini-3-flash-preview")
    .with_text("What's the weather in Tokyo?")
    .create_with_auto_functions()  // Auto-discovers and executes
    .await?;

println!("{}", result.response.as_text().unwrap());
```

### Limiting Available Functions

```rust,ignore
// Only expose specific functions (not all registered ones)
let result = client
    .interaction()
    .with_model("gemini-3-flash-preview")
    .with_text("What's the weather in Tokyo?")
    .add_function(GetWeatherCallable.declaration())  // Only weather
    .create_with_auto_functions()
    .await?;
```

### Multiple Parameters

```rust,ignore
#[tool(
    city(description = "The city name"),
    unit(description = "Temperature unit: celsius or fahrenheit")
)]
fn get_weather_detailed(city: String, unit: String) -> String {
    // Implementation
}
```

### Async Functions

```rust,ignore
#[tool(url(description = "URL to fetch"))]
async fn fetch_url(url: String) -> String {
    // Async operations supported
    reqwest::get(&url).await
        .map(|r| r.text().await.unwrap_or_default())
        .unwrap_or_else(|e| format!(r#"{{"error": "{}"}}"#, e))
}
```

### What the Macro Generates

The `#[tool]` macro generates:
1. A `FunctionDeclaration` from the signature
2. A callable type (e.g., `GetWeatherCallable`)
3. Registration in the global function registry

```rust,ignore
// You can access the generated declaration:
let declaration = GetWeatherCallable.declaration();
println!("Name: {}", declaration.name());
println!("Description: {}", declaration.description());
```

## ToolService for Stateful Functions

Use `ToolService` when functions need shared state like database connections or configuration.

### Implementing ToolService

```rust,ignore
use async_trait::async_trait;
use genai_rs::{CallableFunction, FunctionDeclaration, FunctionError, ToolService};
use std::sync::Arc;

// Your tool with state
struct WeatherTool {
    api_client: Arc<WeatherApiClient>,
}

#[async_trait]
impl CallableFunction for WeatherTool {
    fn declaration(&self) -> FunctionDeclaration {
        FunctionDeclaration::builder("get_weather")
            .description("Get current weather")
            .parameter("city", json!({"type": "string"}))
            .required(vec!["city".to_string()])
            .build()
    }

    async fn call(&self, args: serde_json::Value) -> Result<serde_json::Value, FunctionError> {
        let city = args["city"].as_str().unwrap_or("unknown");
        let weather = self.api_client.get_weather(city).await?;
        Ok(json!({"city": city, "temp": weather.temp}))
    }
}

// The service that provides tools
struct MyToolService {
    api_client: Arc<WeatherApiClient>,
}

impl ToolService for MyToolService {
    fn tools(&self) -> Vec<Arc<dyn CallableFunction>> {
        vec![Arc::new(WeatherTool {
            api_client: self.api_client.clone(),
        })]
    }
}
```

### Using the Service

```rust,ignore
let service = Arc::new(MyToolService {
    api_client: Arc::new(WeatherApiClient::new()),
});

let result = client
    .interaction()
    .with_model("gemini-3-flash-preview")
    .with_text("What's the weather in Tokyo?")
    .with_tool_service(service.clone())  // Inject the service
    .create_with_auto_functions()
    .await?;
```

### Dynamic Configuration

```rust,ignore
use std::sync::RwLock;

struct ConfigurableService {
    precision: Arc<RwLock<u32>>,
}

impl ConfigurableService {
    fn set_precision(&self, value: u32) {
        *self.precision.write().unwrap() = value;
    }
}

// Change config between requests
service.set_precision(8);
let result = client.interaction()
    .with_tool_service(service.clone())
    .create_with_auto_functions()
    .await?;
```

## Manual Function Handling

For full control over execution, handle function calls manually.

### Manual Loop Pattern

Function calls arrive as `Step::FunctionCall { id, name, arguments }` steps. Send
results back as `Step::FunctionResult` steps built with `Step::function_result()`,
which accepts any `Into<FunctionResultPayload>` (a `serde_json::Value`, `&str`,
`String`, or `Vec<Content>`):

```rust,no_run
use genai_rs::{Client, FunctionDeclaration, Step};
use serde_json::json;

# async fn run() -> Result<(), Box<dyn std::error::Error>> {
# let client = Client::new("api-key".to_string());
# fn execute_my_function(_name: &str, _args: &serde_json::Value) -> serde_json::Value {
#     json!({"temp": "22C"})
# }
// Define declarations (schemas only)
let get_weather = FunctionDeclaration::builder("get_weather")
    .description("Get weather for a city")
    .parameter("city", json!({"type": "string"}))
    .required(vec!["city".to_string()])
    .build();

// Initial request
let mut response = client
    .interaction()
    .with_model("gemini-3-flash-preview")
    .with_text("What's the weather in Tokyo?")
    .add_functions(vec![get_weather])
    .create()  // NOT create_with_auto_functions
    .await?;

// Manual execution loop
while response.has_function_calls() {
    let mut results = Vec::new();

    for call in response.function_calls() {
        // YOUR execution logic here
        let result = execute_my_function(call.name, call.args);

        results.push(Step::function_result(
            call.name,
            call.id,  // Required; correlates the result to its call
            result,
        ));
    }

    // Send results back as function_result steps
    response = client
        .interaction()
        .with_model("gemini-3-flash-preview")
        .with_previous_interaction(response.id.as_ref().unwrap())
        .with_history(results)
        .create()
        .await?;
}

// Final text response
println!("{}", response.as_text().unwrap());
# Ok(())
# }
```

For failed executions, use `Step::function_result_error(name, call_id, result)`
which sets `is_error: true` on the step.

### When to Use Manual Handling

| Use Case | Implementation |
|----------|----------------|
| Rate limiting | Add delays between calls |
| Circuit breakers | Track failures, skip calls |
| Caching | Check cache before execution |
| Logging/metrics | Wrap execution with instrumentation |
| Timeouts | Add per-function timeouts |

```rust,ignore
// Example: Rate limiting
for call in response.function_calls() {
    rate_limiter.acquire().await;  // Wait for rate limit
    let result = execute_function(call.name, call.args);
    // ...
}

// Example: Circuit breaker
for call in response.function_calls() {
    if circuit_breaker.is_open(call.name) {
        results.push(Step::function_result_error(call.name, call.id, "circuit open"));
        continue;
    }
    let result = execute_function(call.name, call.args);
    // ...
}
```

## FunctionDeclaration Builder

Build function schemas programmatically.

### Basic Builder

```rust,ignore
use genai_rs::FunctionDeclaration;
use serde_json::json;

let declaration = FunctionDeclaration::builder("search_products")
    .description("Search for products by query")
    .parameter("query", json!({
        "type": "string",
        "description": "Search query"
    }))
    .parameter("limit", json!({
        "type": "integer",
        "description": "Max results (1-100)"
    }))
    .required(vec!["query".to_string()])
    .build();
```

### Complex Parameter Types

```rust,ignore
// Enum parameter
let declaration = FunctionDeclaration::builder("convert_temp")
    .description("Convert temperature")
    .parameter("value", json!({"type": "number"}))
    .parameter("from_unit", json!({
        "type": "string",
        "enum": ["celsius", "fahrenheit", "kelvin"]
    }))
    .parameter("to_unit", json!({
        "type": "string",
        "enum": ["celsius", "fahrenheit", "kelvin"]
    }))
    .required(vec!["value", "from_unit", "to_unit"])
    .build();

// Object parameter
let declaration = FunctionDeclaration::builder("create_user")
    .description("Create a new user")
    .parameter("user", json!({
        "type": "object",
        "properties": {
            "name": {"type": "string"},
            "email": {"type": "string"},
            "age": {"type": "integer"}
        },
        "required": ["name", "email"]
    }))
    .required(vec!["user".to_string()])
    .build();

// Array parameter
let declaration = FunctionDeclaration::builder("process_items")
    .description("Process a list of items")
    .parameter("items", json!({
        "type": "array",
        "items": {"type": "string"}
    }))
    .build();
```

### Accessing Declaration Properties

```rust,ignore
let decl = GetWeatherCallable.declaration();

println!("Name: {}", decl.name());
println!("Description: {}", decl.description());
println!("Parameters: {:?}", decl.parameters());
```

## Function Calling Modes

Control how the model uses functions.

### Available Modes

```rust,ignore
use genai_rs::FunctionCallingMode;

// Auto (default): Model decides whether to call
client.interaction()
    .add_function(decl)
    .with_function_calling_mode(FunctionCallingMode::Auto)

// Any: Model MUST call a function
client.interaction()
    .add_function(decl)
    .with_function_calling_mode(FunctionCallingMode::Any)

// None: Disable function calling
client.interaction()
    .add_function(decl)
    .with_function_calling_mode(FunctionCallingMode::None)

// Validated: Schema adherence for both calls and text
client.interaction()
    .add_function(decl)
    .with_function_calling_mode(FunctionCallingMode::Validated)
```

### Mode Comparison

| Mode | Model Behavior | Use Case |
|------|---------------|----------|
| `Auto` | Decides whether to call | General use |
| `Any` | Must call a function | Guarantee function execution |
| `None` | Cannot call functions | Disable temporarily |
| `Validated` | Schema-strict output | High reliability needs |

On the wire, modes serialize as lowercase strings (`"auto"`, `"any"`, `"none"`,
`"validated"`) in `generation_config.tool_choice`.

### Tool Choice

`with_function_calling_mode()` is a convenience over the underlying
`generation_config.tool_choice` union, which is `Option<ToolChoice>`:

- `ToolChoice::Mode(FunctionCallingMode)` serializes as a plain lowercase string
  (e.g., `"auto"`)
- `ToolChoice::AllowedTools(AllowedTools)` serializes as
  `{"allowed_tools": {"mode": ..., "tools": [...]}}` and restricts the model to a
  named subset of the declared tools

```rust,no_run
use genai_rs::{AllowedTools, Client, FunctionCallingMode, ToolChoice};

# async fn run() -> Result<(), Box<dyn std::error::Error>> {
# let client = Client::new("api-key".to_string());
// Set the union directly
let builder = client.interaction()
    .with_tool_choice(ToolChoice::Mode(FunctionCallingMode::Auto));

// Restrict to a subset of tools (convenience method)
let builder = client.interaction()
    .with_allowed_tools(vec!["get_weather".to_string()]);

// Restrict to a subset AND force a call among them
let builder = client.interaction()
    .with_tool_choice(ToolChoice::allowed_tools(
        Some(FunctionCallingMode::Any),
        vec!["get_weather".to_string(), "get_time".to_string()],
    ));

// Equivalent, via the AllowedTools builder
let builder = client.interaction()
    .with_tool_choice(ToolChoice::AllowedTools(
        AllowedTools::new(vec!["get_weather".to_string()])
            .with_mode(FunctionCallingMode::Any),
    ));
# let _ = builder;
# Ok(())
# }
```

Note: `with_allowed_tools(Vec<String>)` sets the `AllowedTools` form of
`tool_choice` (preserving any previously set mode).

## Streaming Function Call Arguments

When streaming, function-call arguments arrive incrementally as
`StepDelta::ArgumentsDelta` chunks (JSON fragments). The HTTP layer assembles
the streamed `step.start`/`step.delta`/`step.stop` events into the final
`StreamChunk::Completed(response)`, including parsing the accumulated argument
fragments into `Step::FunctionCall { arguments, .. }` — so
`response.function_calls()` works on the completed response after streaming,
exactly as in the non-streaming case. See [Streaming API](STREAMING_API.md).

## Parallel and Compositional Calls

### Parallel Execution

The model may request multiple functions at once:

```rust,ignore
// Model might request: get_weather("Tokyo"), get_weather("London")
for call in response.function_calls() {
    // Execute in parallel using tokio::spawn or futures::join!
}
```

```rust,ignore
use futures::future::join_all;

let futures: Vec<_> = response.function_calls()
    .iter()
    .map(|call| async {
        let result = execute_function(call.name, call.args).await;
        (call.id.to_string(), call.name.to_string(), result)
    })
    .collect();

let results = join_all(futures).await;

// Build one Step::function_result per call and send them back together
let result_steps: Vec<_> = results.into_iter()
    .map(|(id, name, result)| Step::function_result(name, id, result))
    .collect();
```

### Compositional (Chained) Calls

The model chains function outputs:

```text
User: "Convert the temperature in Tokyo to Fahrenheit"
→ Model: get_weather("Tokyo") → {"temp": "22°C"}
→ Model: convert_temp(22, "celsius", "fahrenheit") → {"temp": "71.6°F"}
→ Model: "The temperature in Tokyo is 71.6°F"
```

This happens automatically across multiple loop iterations.

## Best Practices

### 1. Return JSON from Functions

```rust,ignore
#[tool(city(description = "City name"))]
fn get_weather(city: String) -> String {
    // Return JSON for structured data
    format!(r#"{{"city": "{}", "temp": "22°C", "conditions": "sunny"}}"#, city)
}
```

### 2. Handle Errors Gracefully

```rust,ignore
#[tool(id(description = "User ID"))]
fn get_user(id: i32) -> String {
    if id <= 0 {
        return r#"{"error": "Invalid user ID"}"#.to_string();
    }

    match database.find_user(id) {
        Some(user) => serde_json::to_string(&user).unwrap(),
        None => format!(r#"{{"error": "User {} not found"}}"#, id),
    }
}
```

### 3. Validate Inputs

```rust,ignore
#[tool(query(description = "Search query"))]
fn search(query: String) -> String {
    if query.len() > 1000 {
        return r#"{"error": "Query too long"}"#.to_string();
    }

    if query.trim().is_empty() {
        return r#"{"error": "Query cannot be empty"}"#.to_string();
    }

    // Proceed with search...
}
```

### 4. Use Descriptive Names and Descriptions

```rust,ignore
// Good: Clear, specific
#[tool(city(description = "City name (e.g., 'Tokyo', 'New York')"))]
fn get_current_weather(city: String) -> String

// Bad: Vague
#[tool(x(description = "input"))]
fn do_thing(x: String) -> String
```

### 5. Limit Function Count

```rust,ignore
// Provide only relevant functions to reduce model confusion
let result = client
    .interaction()
    .with_text("What's the weather?")
    .add_function(weather_func)  // Only weather, not all 20 functions
    .create_with_auto_functions()
    .await?;
```

### 6. Set Max Loops for Auto Execution

```rust,ignore
// Prevent infinite loops
let result = client
    .interaction()
    .with_text(prompt)
    .add_function(func)
    .with_max_function_call_loops(5)  // Default is 10
    .create_with_auto_functions()
    .await?;
```

## Examples

| Example | Demonstrates |
|---------|-------------|
| `auto_function_calling` | `#[tool]` macro, auto-discovery, modes |
| `tool_service` | Stateful functions, dependency injection |
| `manual_function_calling` | Manual loop, full control |
| `parallel_and_compositional_functions` | Parallel execution, chaining |
| `streaming_auto_functions` | Streaming with auto execution |

Run with:
```bash
cargo run --example <name>
```

## Related Documentation

- [Multi-Turn Function Calling]MULTI_TURN_FUNCTION_CALLING.md - Multi-turn patterns
- [Streaming API]STREAMING_API.md - Streaming with functions
- [Error Handling]ERROR_HANDLING.md - Function errors