Skip to content

Conversation

@TejasGhatte
Copy link
Collaborator

@TejasGhatte TejasGhatte commented Dec 24, 2025

Summary

Added documentation for custom headers in Bedrock SDK and expanded LangChain SDK integration documentation with provider-specific header configuration examples.

Changes

  • Added a new section to Bedrock SDK documentation explaining how to add custom Bifrost headers using boto3's event system
  • Expanded LangChain SDK documentation with detailed examples for adding custom headers to different providers:
    • ChatOpenAI (Python and JavaScript)
    • ChatAnthropic (Python and JavaScript)
    • ChatBedrockConverse (Python, with two implementation methods)
    • ChatGoogleGenerativeAI (Python and JavaScript)
  • Included code samples demonstrating proper header configuration for each provider

Type of change

  • Bug fix
  • Feature
  • Refactor
  • Documentation
  • Chore/CI

Affected areas

  • Core (Go)
  • Transports (HTTP)
  • Providers/Integrations
  • Plugins
  • UI (Next.js)
  • Docs

How to test

  1. Build the documentation site locally:
cd docs
npm install
npm run dev
  1. Navigate to the updated pages:
    • /integrations/bedrock-sdk/overview - Check the new "Adding Custom Headers" section
    • /integrations/langchain-sdk - Verify the expanded "Adding Custom Headers" section

Screenshots/Recordings

N/A

Breaking changes

  • No

Related issues

N/A

Security considerations

Documentation includes examples of header-based authentication and governance mechanisms.

Checklist

  • I read docs/contributing/README.md and followed the guidelines
  • I updated documentation where needed
  • I verified builds succeed (Go and UI)

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 24, 2025

📝 Walkthrough

Summary by CodeRabbit

  • Documentation
    • Enhanced Bedrock SDK integration documentation with custom header injection examples using event-based registration.
    • Expanded LangChain SDK integration documentation with provider-specific header configuration details for OpenAI, Anthropic, Bedrock, and Google Gemini, including code examples in Python and JavaScript.

✏️ Tip: You can customize this high-level summary in your review settings.

Walkthrough

Documentation updates demonstrating custom header injection across Bedrock SDK and LangChain SDK integrations. The Bedrock section covers boto3 event-based header registration, while LangChain section details provider-specific header configuration methods for OpenAI, Anthropic, Bedrock, and Google Gemini providers.

Changes

Cohort / File(s) Summary
Integration Header Configuration Documentation
docs/integrations/bedrock-sdk/overview.mdx, docs/integrations/langchain-sdk.mdx
Added "Adding Custom Headers" section for Bedrock with boto3 event registration example. Expanded LangChain docs with provider-specific header customization patterns (default_headers, additionalHeaders, clientOptions) across OpenAI, Anthropic, Bedrock, and Google Gemini with code examples for both Python and JavaScript.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Headers hop through the wire so bright,
Bifrost guides them left and right,
SDK paths now clearly laid,
Custom headers, neatly made! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly summarizes the main change: adding documentation for passing headers in integration SDKs, which aligns with the changes to Bedrock and LangChain SDK docs.
Description check ✅ Passed The PR description follows the template structure with complete Summary, Changes, Type of change, Affected areas, How to test, Breaking changes, Security considerations, and Checklist sections. All critical documentation sections are filled out appropriately.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 12-24-docs_added_documentation_for_passing_headers_in_integration_sdks

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions
Copy link
Contributor

🧪 Test Suite Available

This PR can be tested by a repository admin.

Run tests for PR #1174

@TejasGhatte TejasGhatte marked this pull request as ready for review December 24, 2025 19:33
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
docs/integrations/bedrock-sdk/overview.mdx (1)

130-130: Consider clarifying the wildcard usage in the note.

The note states "You need to register for each API operation you plan to use (Converse, ConverseStream, InvokeModel, etc.)" which might be interpreted as requiring multiple separate event registrations. However, the code example uses the wildcard bedrock-runtime.* which already covers all operations.

Consider rewording for clarity, such as: "The wildcard * in before-sign.bedrock-runtime.* matches all Bedrock operations (Converse, ConverseStream, InvokeModel, etc.). Alternatively, you can register for specific operations like before-sign.bedrock-runtime.Converse if needed."

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7aa438b and 91645b9.

📒 Files selected for processing (2)
  • docs/integrations/bedrock-sdk/overview.mdx
  • docs/integrations/langchain-sdk.mdx
🧰 Additional context used
📓 Path-based instructions (1)
**

⚙️ CodeRabbit configuration file

always check the stack if there is one for the current PR. do not give localized reviews for the PR, always see all changes in the light of the whole stack of PRs (if there is a stack, if there is no stack you can continue to make localized suggestions/reviews)

Files:

  • docs/integrations/langchain-sdk.mdx
  • docs/integrations/bedrock-sdk/overview.mdx
🔇 Additional comments (3)
docs/integrations/langchain-sdk.mdx (2)

141-142: LGTM!

The introduction clearly sets the expectation that different LangChain provider classes have different methods for adding custom headers.


146-184: LGTM!

The header configuration examples for ChatOpenAI, ChatAnthropic, and ChatGoogleGenerativeAI are well-structured and consistent:

  • Parameter names follow language conventions (default_headers in Python, defaultHeaders in JavaScript)
  • The x-bf-vk header is used consistently across all examples
  • Code examples are syntactically correct and properly demonstrate the configuration patterns

Also applies to: 256-274, 279-338

docs/integrations/bedrock-sdk/overview.mdx (1)

94-129: LGTM!

The custom headers section demonstrates the correct approach using boto3's event system:

  • Uses register_first to ensure headers are added before signing
  • Uses before-sign.bedrock-runtime.* event for proper signature inclusion
  • Uses add_header() method for adding headers
  • Includes helpful comments explaining the purpose

This serves as a good reference implementation for the LangChain documentation.

Comment on lines +186 to +254
### ChatBedrockConverse

For Bedrock models, there are two approaches:

**Method 1: Using the client's event system (after initialization)**

```python
from langchain_aws import ChatBedrockConverse
from langchain_core.messages import HumanMessage

llm = ChatBedrockConverse(
model="us.anthropic.claude-haiku-4-5-20251001-v1:0",
region_name="us-west-2",
endpoint_url="http://localhost:8080/langchain",
aws_access_key_id="dummy-access-key",
aws_secret_access_key="dummy-secret-key",
max_tokens=2000
)

def add_bifrost_headers(request, **kwargs):
"""Add custom headers to Bedrock requests"""
request.headers.add_header("x-bf-vk", "your-virtual-key")

# Register header injection for all Bedrock operations
llm.client.meta.events.register_first(
"before-sign.bedrock-runtime.*",
add_bifrost_headers
)

response = llm.invoke([HumanMessage(content="Hello!")])
print(response.content)
```

**Method 2: Pre-configuring a boto3 client (recommended)**

```python
from langchain_aws import ChatBedrockConverse
from langchain_core.messages import HumanMessage
import boto3

# Create and configure boto3 client
bedrock_client = boto3.client(
service_name="bedrock-runtime",
region_name="us-west-2",
endpoint_url="http://localhost:8080/langchain",
aws_access_key_id="dummy-access-key",
aws_secret_access_key="dummy-secret-key"
)

def add_bifrost_headers(request, **kwargs):
"""Add custom headers to Bedrock requests"""
request.headers["x-bf-vk"] = "your-virtual-key"

# Register header injection before creating LLM
bedrock_client.meta.events.register(
"before-send.bedrock-runtime.*",
add_bifrost_headers
)

# Pass the configured client to ChatBedrockConverse
llm = ChatBedrockConverse(
model="us.anthropic.claude-haiku-4-5-20251001-v1:0",
client=bedrock_client,
max_tokens=2000
)

response = llm.invoke([HumanMessage(content="Hello!")])
print(response.content)
```
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Standardize event hooks and header addition methods across both Bedrock approaches.

The two methods shown for ChatBedrockConverse have critical inconsistencies:

  1. Event timing mismatch: Method 1 uses before-sign while Method 2 uses before-send. The before-send event fires after AWS Signature V4 signing, meaning custom headers added at that point aren't included in the signature. This could cause authentication or validation issues with Bifrost.

  2. Registration method: Method 1 uses register_first while Method 2 uses register. Using register_first ensures headers are added before any other event handlers, which is important for signature inclusion.

  3. Header addition syntax: Method 1 uses request.headers.add_header() while Method 2 uses dictionary assignment request.headers["x-bf-vk"].

For consistency with the Bedrock SDK documentation (bedrock-sdk/overview.mdx) and to ensure headers are properly signed, both methods should use before-sign.bedrock-runtime.* with register_first and add_header().

🔎 Proposed fix for Method 2
 # Register header injection before creating LLM
-bedrock_client.meta.events.register(
-    "before-send.bedrock-runtime.*",
+bedrock_client.meta.events.register_first(
+    "before-sign.bedrock-runtime.*",
     add_bifrost_headers
 )

Also update the header addition to use add_header() for consistency:

 def add_bifrost_headers(request, **kwargs):
     """Add custom headers to Bedrock requests"""
-    request.headers["x-bf-vk"] = "your-virtual-key"
+    request.headers.add_header("x-bf-vk", "your-virtual-key")
🤖 Prompt for AI Agents
In docs/integrations/langchain-sdk.mdx around lines 186 to 254, the two
ChatBedrockConverse examples are inconsistent and may produce unsigned custom
headers: change Method 2 to use the same event name and timing as Method 1
("before-sign.bedrock-runtime.*"), use register_first for registration in both
examples, and use request.headers.add_header("x-bf-vk", "...") (instead of dict
assignment) so headers are added prior to signing; also ensure the event
registration in Method 2 happens before passing the client into
ChatBedrockConverse.

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