-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Description
Checked other resources
- This is a bug, not a usage question.
- I added a clear and descriptive title that summarizes this issue.
- I used the GitHub search to find a similar question and didn't find it.
- I am sure that this is a bug in LangChain rather than my code.
- The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
- This is not related to the langchain-community package.
- I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
- I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.
Example Code
from langchain.agents import create_agent from langchain_anthropic import ChatAnthropic # Create model model = ChatAnthropic( model="claude-sonnet-4-20250514", api_key="test-key", ) # Claude Code OAuth requires system_prompt as array of message blocks system_array = [ {"type": "text", "text": "You are Claude Code, Anthropic's official CLI for Claude."}, {"type": "text", "text": "You are a security auditor.", "cache_control": {"type": "ephemeral"}}, ] # This doesn't work - create_agent() only accepts string for system_prompt agent = create_agent( model=model, tools=[], system_prompt=system_array # TypeError or validation error )
Error Message and Stack Trace (if applicable)
The silent error is that everything is concatenated into a single message, but anthropic can use
[
{
"text": "Today's date is 2024年06月01日.",
"type": "text"
}
]
Which is needed for some things and how it's supported by vercel ai sdk
Description
I'm trying to use langchain.agents.create_agent() with system prompts that leverage multiple text blocks with different cache control settings.
Expected behavior: create_agent(system_prompt=...) should support both string and array formats, matching the Anthropic API spec:
# String format (current support) system_prompt="You are a helpful assistant." # Array format (not currently supported) system_prompt=[ {"type": "text", "text": "Base instructions"}, {"type": "text", "text": "Dynamic instructions", "cache_control": {"type": "ephemeral"}} ]
Actual behavior: create_agent() only accepts system prompts as strings. This prevents using advanced Anthropic API features like separate cache control per text block.
Current workaround: Concatenating all text blocks into a single string:
# Build system prompt as concatenated string system_prompt = "Base instructions\n\nDynamic instructions" agent = create_agent( model=model, tools=[], system_prompt=system_prompt # Concatenated string instead of array )
This works but loses the ability to use separate cache control blocks and proper message structure that the Anthropic API supports.
System Info
langchain-anthropic==0.3.9 langchain-core==0.3.32 anthropic==0.42.0 python==3.12