Skip to content

Conversation

@nazq
Copy link
Contributor

@nazq nazq commented Dec 16, 2025

Summary

  • Adds extra_headers() builder method to LLMBuilder for runtime-configurable HTTP headers
  • Enables use cases like Cloudflare Access authentication tokens for secure vLLM endpoints
  • Headers are applied to all HTTP requests made by OpenAI-compatible providers

Note: This PR is based on #94 (streaming with tools support) because the new chat_stream_with_tools() methods also make HTTP requests that need extra headers support.

Use Case

use std::collections::HashMap;
use llm::builder::LLMBuilder;

let headers = HashMap::from([
    ("CF-Access-Client-Id".to_string(), "my-client-id".to_string()),
    ("CF-Access-Client-Secret".to_string(), "my-secret".to_string()),
]);

let llm = LLMBuilder::new()
    .backend(LLMBackend::OpenAI)
    .base_url("https://vllm.example.com/v1/")
    .api_key("not-needed")
    .model("meta-llama/Llama-3.1-8B-Instruct")
    .extra_headers(headers)
    .build()?;

Changes

  • src/builder.rs - Added extra_headers field and builder method
  • src/providers/openai_compatible.rs - Store and apply extra headers in requests
  • src/backends/openai.rs - Pass extra headers to provider
  • Other backends updated to pass None for compatibility

Test plan

  • Build passes
  • Clippy passes
  • Unit tests for extra_headers functionality added
  • Manual testing with Cloudflare-protected endpoint

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds runtime-configurable HTTP headers support for LLM providers, enabling use cases like Cloudflare Access authentication. The PR also includes streaming with tools functionality from PR #94.

Key Changes:

  • Added extra_headers() builder method to configure runtime HTTP headers
  • Implemented chat_stream_with_tools() for streaming responses with tool call support
  • Added StreamChunk enum for unified streaming events (text, tool calls, completion)

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/builder.rs Added extra_headers field and builder method; passed to OpenAI and Cohere backends
src/providers/openai_compatible.rs Stored extra_headers field; applied headers to all chat/streaming requests; implemented chat_stream_with_tools() with SSE parsing
src/backends/openai.rs Passed extra_headers to provider; added extra_headers to hosted tools endpoint; delegated chat_stream_with_tools() to provider
src/backends/mistral.rs Added None for extra_headers parameter (not exposed)
src/backends/groq.rs Added None for extra_headers parameter (not exposed)
src/backends/cohere.rs Added None for extra_headers parameter (not exposed)
src/backends/huggingface.rs Added None for extra_headers parameter (not exposed)
src/backends/openrouter.rs Added None for extra_headers parameter (not exposed)
src/backends/anthropic.rs Implemented chat_stream_with_tools() with Anthropic-specific SSE parsing; handles empty tool arguments correctly
src/chat/mod.rs Added StreamChunk enum and chat_stream_with_tools() trait method
src/resilient_llm.rs Added resilient wrappers for new streaming methods
tests/test_backends.rs Added comprehensive integration tests for streaming with tools (Anthropic backend)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@graniet
Copy link
Owner

graniet commented Dec 19, 2025

Hey @nazq, thanks for your work. Could you resolve the conflict?

@nazq
Copy link
Contributor Author

nazq commented Dec 19, 2025

Sure. Give me a couple hours

@nazq nazq force-pushed the feature/extra-headers branch from 33aff7d to ae26883 Compare December 19, 2025 13:35
@nazq
Copy link
Contributor Author

nazq commented Dec 19, 2025

Hey @nazq, thanks for your work. Could you resolve the conflict?

Done

Add extra_headers option to LLMBuilder for custom HTTP headers in all requests.
Useful for custom authentication like Cloudflare Access tokens.

- Add extra_headers field and builder method to LLMBuilder
- Add extra_headers to OpenAICompatibleProvider and apply in all request methods
- Pass extra_headers from builder through OpenAI backend
- Update all other OpenAI-compatible backends with None default
- Add unit tests for extra_headers functionality
@nazq nazq force-pushed the feature/extra-headers branch from ae26883 to 198ad7a Compare December 31, 2025 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants