langchain_google_vertexai.utils create_context_cache not working , create_context_cache passes GAPIC ToolConfig, but CachedContent.create requires SDK vertexai.generative_models.ToolConfig
#1,241 opened on Oct 11, 2025
Repository metrics
- Stars
- (392 stars)
- PR merge metrics
- (PR metrics pending)
Description
Summary
langchain_google_vertexai.utils.create_context_cache(...) accepts a dict-like tool_config and (per docs) converts it for you. However, it ultimately calls the Vertex SDK vertexai.preview.caching.CachedContent.create(...), which expects an SDK vertexai.generative_models.ToolConfig instance. Passing a dict (converted to GAPIC ToolConfig) triggers:
ValueError: tool_config must be a ToolConfig object
This appears to be a type mismatch between what the LangChain helper builds and what the Vertex SDK context-cache API requires.
Why this looks like a bug
LangChain’s API reference for create_context_cache documents tool_config as an internal dict (_ToolConfigDict) that the helper accepts and converts.
Vertex AI’s SDK docs show CachedContent.create(...) and function calling expect SDK types (vertexai.generative_models.ToolConfig, with FunctionCallingConfig).
Actual behavior
create_context_cache(...) raises tool_config must be a ToolConfig object when tool_config is provided as a dict (even though the helper API advertises accepting a dict and doing conversion).
Expected behavior
create_context_cache(...) should convert the dict into an SDK vertexai.generative_models.ToolConfig before calling vertexai.preview.caching.CachedContent.create(...), or otherwise route through an API that accepts a GAPIC ToolConfig. In other words, the helper should “just work” with the documented dict input.
Minimal repro
from langchain_google_vertexai import ChatVertexAI
from langchain_google_vertexai.utils import create_context_cache
from langchain_core.messages import SystemMessage
chat = ChatVertexAI(model="gemini-1.5-pro", project="...", location="...")
messages = [SystemMessage(content="Long system instructions here")]
tools = [{"function": {"name": "my_tool", "parameters": {"type": "object", "properties": {}}}}]
tool_config = {
"function_calling_config": {
"mode": 1, # ANY
"allowed_function_names": ["my_tool"]
}
}
cache_name = create_context_cache(
model=chat,
messages=messages,
tools=tools,
tool_config=tool_config,
)