ai-sdk-provider-utils 0.3.0

Utility functions and helpers for AI SDK providers
Documentation

AI SDK Provider Utilities

Common utility functions for implementing AI SDK providers.

Overview

This crate provides shared functionality for provider implementations, including HTTP header management, request/response handling, and other cross-cutting concerns. These utilities promote code reuse and consistency across different provider implementations.

Features

  • Header Management - Merge and manage HTTP headers for API requests
  • Reqwest Integration - Optional integration with the reqwest HTTP client

Usage

Add this to your Cargo.toml:

[dependencies]
ai-sdk-provider-utils = "0.1.0"

For reqwest integration:

[dependencies]
ai-sdk-provider-utils = { version = "0.1.0", features = ["reqwest"] }

Header Management

The crate provides utilities for merging base headers with request-specific overrides:

use ai_sdk_provider_utils::merge_headers;
use std::collections::HashMap;

let mut base_headers = HashMap::new();
base_headers.insert("Authorization".to_string(), "Bearer token".to_string());

let mut custom_headers = HashMap::new();
custom_headers.insert("X-Custom-Header".to_string(), "value".to_string());

let merged = merge_headers(base_headers, Some(&custom_headers));

Reqwest Integration

When using the reqwest feature, you can directly create a reqwest::header::HeaderMap:

use ai_sdk_provider_utils::merge_headers_reqwest;
use std::collections::HashMap;

let mut base_headers = HashMap::new();
base_headers.insert("Authorization".to_string(), "Bearer token".to_string());

let mut custom_headers = HashMap::new();
custom_headers.insert("X-Custom-Header".to_string(), "value".to_string());

let headers = merge_headers_reqwest(base_headers, Some(&custom_headers));

// Use directly with reqwest
let response = reqwest::Client::new()
    .post("https://api.example.com/generate")
    .headers(headers)
    .json(&request_body)
    .send()
    .await?;

Use Cases

This crate is intended for use by:

  • AI SDK provider implementation crates (ai-sdk-openai, ai-sdk-anthropic, etc.)
  • Custom provider implementations that want consistent header handling
  • Any code that needs to merge HTTP headers with override semantics

License

See the main AI SDK repository for license information.