-
Notifications
You must be signed in to change notification settings - Fork 150
docs: added documentation for passing headers in integration sdks #1174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
docs: added documentation for passing headers in integration sdks #1174
Conversation
📝 WalkthroughSummary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings. WalkthroughDocumentation 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
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. Comment |
🧪 Test Suite AvailableThis PR can be tested by a repository admin. |
There was a problem hiding this 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
*inbefore-sign.bedrock-runtime.*matches all Bedrock operations (Converse, ConverseStream, InvokeModel, etc.). Alternatively, you can register for specific operations likebefore-sign.bedrock-runtime.Converseif needed."
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
docs/integrations/bedrock-sdk/overview.mdxdocs/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.mdxdocs/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_headersin Python,defaultHeadersin JavaScript)- The
x-bf-vkheader 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_firstto 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.
| ### 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) | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Standardize event hooks and header addition methods across both Bedrock approaches.
The two methods shown for ChatBedrockConverse have critical inconsistencies:
-
Event timing mismatch: Method 1 uses
before-signwhile Method 2 usesbefore-send. Thebefore-sendevent 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. -
Registration method: Method 1 uses
register_firstwhile Method 2 usesregister. Usingregister_firstensures headers are added before any other event handlers, which is important for signature inclusion. -
Header addition syntax: Method 1 uses
request.headers.add_header()while Method 2 uses dictionary assignmentrequest.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.

Summary
Added documentation for custom headers in Bedrock SDK and expanded LangChain SDK integration documentation with provider-specific header configuration examples.
Changes
Type of change
Affected areas
How to test
cd docs npm install npm run dev/integrations/bedrock-sdk/overview- Check the new "Adding Custom Headers" section/integrations/langchain-sdk- Verify the expanded "Adding Custom Headers" sectionScreenshots/Recordings
N/A
Breaking changes
Related issues
N/A
Security considerations
Documentation includes examples of header-based authentication and governance mechanisms.
Checklist
docs/contributing/README.mdand followed the guidelines