tiny-agent-rs 0.1.0

A lightweight, type-safe Rust agent library for LLM tool calling
Documentation
# Tool Schema Compatibility Fix

## Issue Summary

The tinyagent-rust agent was experiencing "Invalid input" errors when attempting to use tools with OpenRouter API, despite successful API authentication and basic chat functionality working perfectly.

### What Worked
- ✅ API key authentication
- ✅ Model selection and connectivity  
- ✅ Basic chat completions without tools
- ✅ curl requests worked perfectly

### What Failed
- ❌ Agent requests with tools included
- ❌ Error: `{"code":400,"message":"Invalid input"}`

## Root Cause Analysis

### 1. Model Compatibility Issue
The agent was configured to use `microsoft/wizardlm-2-8x22b` as the default model, which does not support the OpenAI tool calling format. While this model works fine for plain chat completions, it rejects requests that include OpenAI-style tool schemas.

### 2. Tool Response Format Issue  
The agent was sending tool results as JSON objects instead of strings. The OpenAI API expects tool response content to be a string, not a JSON object.

**Incorrect format:**
```json
{
  "role": "tool",
  "tool_call_id": "call_abc123",
  "name": "calculator", 
  "content": {"result": 120.0, "operation": "multiply"}
}
```

**Correct format:**
```json
{
  "role": "tool", 
  "tool_call_id": "call_abc123",
  "name": "calculator",
  "content": "{\"result\": 120.0, \"operation\": \"multiply\"}"
}
```

### 3. Multi-Tool Call Handling Issue
When the assistant made multiple tool calls in a single response, the agent was only processing the first tool call and ignoring the rest. This caused API errors like:
```
"No tool output found for function call call_abc123."
```

## Solution Implementation

### 1. Switch to Compatible Model
Changed the default model from `microsoft/wizardlm-2-8x22b` to `openai/gpt-4.1-mini` which fully supports OpenAI tool calling format.

**Files changed:**
- `src/agent.rs` - Updated default model
- `examples/complete_agent.rs` - Updated example model
- `examples/test_openrouter.rs` - Updated test model

### 2. Fix Tool Response Format
Modified the agent to convert tool results to strings before sending to the API.

**Code change in `src/agent.rs`:**
```rust
// Before
"content": result

// After  
"content": result.to_string()
```

### 3. Implement Multi-Tool Call Support
Refactored the tool call handling logic to process ALL tool calls in the response array, not just the first one.

**Code change in `src/agent.rs`:**
```rust
// Before - only processed first tool call
if let Some(tool_call) = tool_calls.as_array().and_then(|arr| arr.first()) {
    // process single tool call
}

// After - process all tool calls
if let Some(tool_calls_array) = tool_calls.as_array() {
    for tool_call in tool_calls_array {
        // process each tool call
    }
}
```

### 4. Clean Up Code Quality
Fixed compiler warnings by prefixing unused variables with underscore.

## Verification

After implementing the fixes, all agent tests now pass successfully:

### ✅ Test 1: Complex Calculation
**Prompt:** "Calculate (15 * 8) + (100 / 5) - 22"
**Result:** Successfully executed multiple calculator tool calls and returned correct answer: 118

### ✅ Test 2: Weather Information  
**Prompt:** "What's the weather like in London and Tokyo?"
**Result:** Successfully made parallel tool calls to both locations and provided comprehensive weather data

### ✅ Test 3: Multi-Tool Query
**Prompt:** "If it's 25°C in London and 15°C in New York, what's the temperature difference in Fahrenheit?"
**Result:** Coordinated multiple calculator tool calls to perform temperature conversion and calculation

### ✅ Test 4: General Knowledge
**Prompt:** "What are the main differences between Rust and Go programming languages?"
**Result:** Successfully handled question without tools (no tool calls needed)

## Technical Details

### API Request Flow
1. Agent creates request with tool schemas
2. OpenAI API returns tool call instructions  
3. Agent executes ALL requested tool calls
4. Agent returns tool results as strings
5. OpenAI API processes results and provides final answer

### Key Learnings
- **Model compatibility is crucial** - Not all models support OpenAI tool format
- **API contract compliance** - Tool responses must be strings, not objects
- **Complete processing** - All tool calls must be handled, not just the first one
- **Error messages are helpful** - OpenAI provides clear error messages for debugging

## Files Modified

- `src/agent.rs` - Core agent logic fixes
- `examples/complete_agent.rs` - Updated model configuration
- `examples/test_openrouter.rs` - Updated test model  
- `src/cli.rs` - Fixed compiler warning

## Conclusion

The tool schema compatibility issue was successfully resolved by:
1. Using a model that supports OpenAI tool format
2. Ensuring proper API contract compliance for tool responses
3. Implementing complete multi-tool call processing

The agent now provides robust tool calling capabilities with proper error handling and multi-tool coordination.