🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Extend Prompt Caching Support to More Providers (#3901)
Xiangyu Shi8e4b179bac69
diff --git a/camel/configs/bedrock_config.py b/camel/configs/bedrock_config.pyindex f54a581d5..dd69478af 100644--- a/camel/configs/bedrock_config.py+++ b/camel/configs/bedrock_config.py@@ -11,7 +11,7 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========-from typing import Dict, Optional, Union+from typing import Dict, Literal, Optional, Unionfrom camel.configs.base_config import BaseConfig@@ -35,6 +35,9 @@ class BedrockConfig(BaseConfig):the tokens comprising the top 10% probability mass are considered.(default: :obj:`None`)top_k (int, optional): The number of top tokens to consider.+ stop (list[str], optional): Up to 4 sequences where the API will+ stop generating further tokens. The returned text will not contain+ the stop sequence. (default: :obj:`None`)stream (bool, optional): If True, partial message deltas will be sentas data-only server-sent events as they become available.(default: :obj:`None`)@@ -59,17 +62,24 @@ class BedrockConfig(BaseConfig)::obj:`o1mini`, :obj:`o1preview`, :obj:`o3mini`). If not providedor if the model type does not support it, this parameter isignored. (default: :obj:`None`)+ cache_control (Optional[Literal["5m", "1h"]], optional): Cache TTL+ for Bedrock prompt caching. Set to :obj:`"5m"` for 5-minute or+ :obj:`"1h"` for 1-hour TTL (only supported by some models).+ Cache checkpoints are inserted on both the system message and+ the last user message. (default: :obj:`None`)"""max_tokens: Optional[int] = Nonetemperature: Optional[float] = Nonetop_p: Optional[float] = Nonetop_k: Optional[int] = None+ stop: Optional[list] = Nonestream: Optional[bool] = Nonetool_choice: Optional[Union[Dict[str, Union[str, Dict[str, str]]], str]] = Nonereasoning_effort: Optional[str] = None+ cache_control: Optional[Literal["5m", "1h"]] = NoneBEDROCK_API_PARAMS = {param for param in BedrockConfig.model_fields.keys()}diff --git a/camel/configs/gemini_config.py b/camel/configs/gemini_config.pyindex 37ab730a4..b280618ba 100644--- a/camel/configs/gemini_config.py+++ b/camel/configs/gemini_config.py@@ -73,6 +73,15 @@ class GeminiConfig(BaseConfig):forces the model to call that tool. :obj:`"none"` is the defaultwhen no tools are present. :obj:`"auto"` is the default if toolsare present.+ cache_control (str, optional): Reserved cache TTL field for API+ compatibility. Gemini explicit caches are created via+ :obj:`GeminiModel.create_cache()`, and this field is not applied+ automatically to requests. Format: "300s", "3600s", etc.+ (default: :obj:`None`)+ cached_content (str, optional): Reference to an existing cache to use.+ Format: "cachedContents/{cache_id}". When set, the model will use+ the cached content instead of re-processing the same context.+ (default: :obj:`None`)"""temperature: Optional[float] = None # openai default: 1.0@@ -85,6 +94,8 @@ class GeminiConfig(BaseConfig):tool_choice: Optional[Union[Dict[str, Union[str, Dict[str, str]]], str]] = None+ cache_control: Optional[str] = None+ cached_content: Optional[str] = NoneGemini_API_PARAMS = {param for param in GeminiConfig.model_fields.keys()}diff --git a/camel/models/__init__.py b/camel/models/__init__.pyindex 85190a2e6..eb555789a 100644--- a/camel/models/__init__.py+++ b/camel/models/__init__.py@@ -17,6 +17,7 @@ from .amd_model import AMDModelfrom .anthropic_model import AnthropicModelfrom .atlascloud_model import AtlasCloudModelfrom .avian_model import AvianModel+from .aws_bedrock_converse_model import AWSBedrockConverseModelfrom .aws_bedrock_model import AWSBedrockModelfrom .azure_openai_model import AzureOpenAIModelfrom .base_audio_model import BaseAudioModel@@ -99,6 +100,7 @@ __all__ = ['YiModel','QwenModel','AWSBedrockModel',+ 'AWSBedrockConverseModel','ModelProcessingError','NebiusModel','DeepSeekModel',diff --git a/camel/models/aws_bedrock_converse_model.py b/camel/models/aws_bedrock_converse_model.pynew file mode 100644index 000000000..6d40cb3ce--- /dev/null+++ b/camel/models/aws_bedrock_converse_model.py@@ -0,0 +1,797 @@+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========++import base64+import json+import os+import re+import time+import warnings+from typing import (+ Any,+ Dict,+ Generator,+ List,+ Optional,+ Tuple,+ Type,+ Union,+)+from uuid import uuid4++from pydantic import BaseModel++from camel.configs import BedrockConfig+from camel.messages import OpenAIMessage+from camel.models.base_model import BaseModelBackend+from camel.types import (+ ChatCompletion,+ ChatCompletionChunk,+ ModelType,+)+from camel.utils import (+ BaseTokenCounter,+ OpenAITokenCounter,+ api_keys_required,+)++if os.environ.get("LANGFUSE_ENABLED", "False").lower() == "true":+ try:+ from langfuse.decorators import observe+ except ImportError:+ from camel.utils import observe+else:+ from camel.utils import observe+++class AWSBedrockConverseModel(BaseModelBackend):+ r"""AWS Bedrock Converse API backend with prompt caching support.++ Args:+ model_type (Union[ModelType, str]): Model for which a backend is+ created.+ model_config_dict (Dict[str, Any], optional): A dictionary that will+ be fed into the backend constructor. If :obj:`None`,+ :obj:`BedrockConfig().as_dict()` will be used.+ (default: :obj:`None`)+ api_key (str, optional): The API key for authenticating with the+ AWS Bedrock service. (default: :obj:`None`)+ region_name (str, optional): The AWS region name, e.g.+ ``"us-east-1"``. (default: :obj:`None`)+ aws_access_key_id (str, optional): Explicit IAM access key ID.+ (default: :obj:`None`)+ aws_secret_access_key (str, optional): Explicit IAM secret access+ key. (default: :obj:`None`)+ aws_session_token (str, optional): Temporary session token for STS+ credentials. (default: :obj:`None`)+ token_counter (BaseTokenCounter, optional): Token counter to use+ for the model. If not provided, :obj:`OpenAITokenCounter(+ ModelType.GPT_4O_MINI)` will be used.+ (default: :obj:`None`)+ timeout (float, optional): The timeout value in seconds for API+ calls. If not provided, will fall back to the MODEL_TIMEOUT+ environment variable or default to 180 seconds.+ (default: :obj:`None`)+ max_retries (int, optional): Maximum number of retries for API+ calls. (default: :obj:`3`)+ **kwargs (Any): Additional arguments to pass to the client+ initialization.++ References:+ https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html+ """++ @api_keys_required(+ [+ ("region_name", "AWS_REGION"),+ ]+ )+ def __init__(+ self,+ model_type: Union[ModelType, str],+ model_config_dict: Optional[Dict[str, Any]] = None,+ api_key: Optional[str] = None,+ region_name: Optional[str] = None,+ aws_access_key_id: Optional[str] = None,+ aws_secret_access_key: Optional[str] = None,+ aws_session_token: Optional[str] = None,+ token_counter: Optional[BaseTokenCounter] = None,+ timeout: Optional[float] = None,+ max_retries: int = 3,+ **kwargs: Any,+ ) -> None:+ if model_config_dict is None:+ model_config_dict = BedrockConfig().as_dict()+ api_key = (+ api_key+ or os.environ.get("AWS_BEARER_TOKEN_BEDROCK")+ or os.environ.get("BEDROCK_API_KEY")+ or None+ )+ region_name = region_name or os.environ.get("AWS_REGION")++ self._cache_ttl = model_config_dict.pop("cache_control", None)++ if self._cache_ttl is not None and self._cache_ttl not in (+ "5m",+ "1h",+ ):+ raise ValueError(+ f"Invalid cache_control value: {self._cache_ttl!r}. "+ "Must be either '5m' or '1h'."+ )++ super().__init__(+ model_type=model_type,+ model_config_dict=model_config_dict,+ api_key=api_key,+ url=None,+ token_counter=token_counter,+ timeout=timeout or float(os.environ.get("MODEL_TIMEOUT", 180)),+ max_retries=max_retries,+ )++ bedrock_client = kwargs.pop("bedrock_client", None)+ self._bedrock_client = bedrock_client+ self._region_name = region_name+ self._aws_access_key_id = aws_access_key_id or os.environ.get(+ "AWS_ACCESS_KEY_ID"+ )+ self._aws_secret_access_key = aws_secret_access_key or os.environ.get(+ "AWS_SECRET_ACCESS_KEY"+ )+ self._aws_session_token = aws_session_token or os.environ.get(+ "AWS_SESSION_TOKEN"+ )++ @property+ def bedrock_client(self) -> Any:+ if self._bedrock_client is None:+ client_kwargs: Dict[str, Any] = {}+ if self._region_name is not None:+ client_kwargs["region_name"] = self._region_name++ if self._api_key:+ # Bearer Token authentication via env var.+ # boto3 does not support passing bearer tokens as a+ # session/client parameter yet; env var is the only way.+ # See: https://github.com/boto/boto3/issues/4723+ os.environ["AWS_BEARER_TOKEN_BEDROCK"] = self._api_key+ elif self._aws_access_key_id and self._aws_secret_access_key:+ # Explicit IAM credentials+ client_kwargs["aws_access_key_id"] = self._aws_access_key_id+ client_kwargs["aws_secret_access_key"] = (+ self._aws_secret_access_key+ )+ if self._aws_session_token:+ client_kwargs["aws_session_token"] = (+ self._aws_session_token+ )+ # Otherwise: boto3 default credential chain+ # (env vars, ~/.aws/credentials, instance profile, etc.)++ import boto3++ self._bedrock_client = boto3.client(+ "bedrock-runtime",+ **client_kwargs,+ )+ return self._bedrock_client++ @property+ def token_counter(self) -> BaseTokenCounter:+ if not self._token_counter:+ self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)+ return self._token_counter++ @property+ def stream(self) -> bool:+ return self.model_config_dict.get("stream", False)++ @staticmethod+ def _new_cache_point(ttl: Optional[str] = None) -> Dict[str, Any]:+ # The Bedrock default cache TTL is 5 minutes. When a non-default+ # TTL is requested (e.g. "1h") we must include it explicitly in+ # the cache point; otherwise the "ttl" key is omitted and the+ # service uses its 5-minute default.+ cp: Dict[str, Any] = {"type": "default"}+ if ttl and ttl != "5m":+ cp["ttl"] = ttl+ return {"cachePoint": cp}++ @staticmethod+ def _parse_json_or_text(value: Any) -> Dict[str, Any]:+ if isinstance(value, dict):+ return {"json": value}+ if isinstance(value, list):+ return {"json": value}+ if isinstance(value, str):+ stripped = value.strip()+ if not stripped:+ return {"text": ""}+ try:+ parsed = json.loads(stripped)+ if isinstance(parsed, (dict, list)):+ return {"json": parsed}+ return {"text": stripped}+ except Exception:+ return {"text": value}+ return {"text": str(value)}++ @staticmethod+ def _extract_text(content: Any) -> str:+ if isinstance(content, str):+ return content+ if isinstance(content, list):+ chunks: List[str] = []+ for item in content:+ if isinstance(item, str):+ chunks.append(item)+ elif isinstance(item, dict):+ txt = item.get("text")+ if isinstance(txt, str):+ chunks.append(txt)+ return "".join(chunks)+ return str(content)++ @staticmethod+ def _parse_data_image(url: str) -> Optional[Dict[str, Any]]:+ # Bedrock Converse supports jpeg, png, gif, and webp image formats.+ # Only base64-encoded data URLs are converted here.+ m = re.match(r"^data:image/([a-zA-Z0-9+.-]+);base64,(.+)$", url)+ if not m:+ return None+ image_format = m.group(1).lower().replace("jpg", "jpeg")+ payload = m.group(2)+ try:+ image_bytes = base64.b64decode(payload, validate=True)+ except Exception:+ return None+ return {+ "image": {+ "format": image_format,+ "source": {"bytes": image_bytes},+ }+ }++ def _to_bedrock_content_blocks(+ self,+ content: Any,+ ) -> List[Dict[str, Any]]:+ if isinstance(content, str):+ return [{"text": content}] if content else []++ blocks: List[Dict[str, Any]] = []+ if not isinstance(content, list):+ return blocks++ for item in content:+ if isinstance(item, str):+ blocks.append({"text": item})+ continue+ if not isinstance(item, dict):+ continue++ if isinstance(item.get("text"), str):+ blocks.append({"text": item["text"]})+ continue++ if item.get("type") == "image_url":+ image_url = item.get("image_url", {})+ if isinstance(image_url, dict):+ url = image_url.get("url")+ if isinstance(url, str):+ image_block = self._parse_data_image(url)+ if image_block:+ blocks.append(image_block)+ else:+ warnings.warn(+ "Only data URL images are supported for "+ "Bedrock Converse image content. Skipping "+ "non-data URL image.",+ UserWarning,+ stacklevel=3,+ )+ continue++ if "image" in item and isinstance(item["image"], dict):+ blocks.append({"image": item["image"]})++ return blocks++ def _convert_openai_to_bedrock_messages(+ self,+ messages: List[OpenAIMessage],+ ) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:+ system_parts: List[str] = []+ bedrock_messages: List[Dict[str, Any]] = []++ for msg in messages:+ role = msg.get("role")+ if role == "system":+ system_parts.append(self._extract_text(msg.get("content")))+ continue++ if role in ("user", "assistant"):+ content_blocks = self._to_bedrock_content_blocks(+ msg.get("content")+ )++ if role == "assistant":+ tool_calls = msg.get("tool_calls")+ if isinstance(tool_calls, list):+ for call in tool_calls:+ if not isinstance(call, dict):+ continue+ function = call.get("function", {})+ if not isinstance(function, dict):+ continue+ name = function.get("name")+ if not isinstance(name, str) or not name:+ continue+ call_id = (+ call.get("id") or f"tool_{uuid4().hex[:8]}"+ )+ raw_args = function.get("arguments", "{}")+ try:+ parsed_args = (+ json.loads(raw_args)+ if isinstance(raw_args, str)+ else raw_args+ )+ except Exception:+ parsed_args = {}+ content_blocks.append(+ {+ "toolUse": {+ "toolUseId": str(call_id),+ "name": name,+ "input": parsed_args+ if isinstance(parsed_args, dict)+ else {},+ }+ }+ )++ if content_blocks:+ bedrock_messages.append(+ {"role": role, "content": content_blocks}+ )+ continue++ if role == "tool":+ tool_id = msg.get("tool_call_id")+ if not isinstance(tool_id, str) or not tool_id:+ continue+ payload = self._parse_json_or_text(msg.get("content", ""))+ bedrock_messages.append(+ {+ "role": "user",+ "content": [+ {+ "toolResult": {+ "toolUseId": tool_id,+ "content": [payload],+ }+ }+ ],+ }+ )++ system_blocks: List[Dict[str, Any]] = []+ if system_parts:+ system_blocks = [+ {"text": "\n".join(part for part in system_parts if part)}+ ]++ if self._cache_ttl:+ if system_blocks:+ system_blocks.append(self._new_cache_point(self._cache_ttl))++ for bedrock_msg in reversed(bedrock_messages):+ if bedrock_msg.get("role") == "user":+ msg_content = bedrock_msg.get("content")+ if isinstance(msg_content, list):+ msg_content.append(+ self._new_cache_point(self._cache_ttl)+ )+ break++ return system_blocks, bedrock_messages++ def _convert_openai_tools_to_bedrock(+ self,+ tools: Optional[List[Dict[str, Any]]],+ ) -> Optional[List[Dict[str, Any]]]:+ if not tools:+ return None++ bedrock_tools: List[Dict[str, Any]] = []+ for tool in tools:+ if not isinstance(tool, dict):+ continue+ function = tool.get("function")+ if not isinstance(function, dict):+ continue+ name = function.get("name")+ if not isinstance(name, str) or not name:+ continue+ description = function.get("description", "")+ params = function.get("parameters", {"type": "object"})+ if not isinstance(params, dict):+ params = {"type": "object"}++ bedrock_tools.append(+ {+ "toolSpec": {+ "name": name,+ "description": description,+ "inputSchema": {"json": params},+ }+ }+ )++ return bedrock_tools or None++ def _convert_tool_choice(+ self, tool_choice: Any+ ) -> Optional[Dict[str, Any]]:+ if tool_choice is None:+ return None+ if tool_choice == "auto":+ return {"auto": {}}+ if tool_choice == "required":+ return {"any": {}}+ if tool_choice == "none":+ return None+ if isinstance(tool_choice, dict):+ if tool_choice.get("type") == "function":+ function = tool_choice.get("function", {})+ if isinstance(function, dict) and isinstance(+ function.get("name"), str+ ):+ return {"tool": {"name": function["name"]}}+ if "tool" in tool_choice:+ return {"tool": tool_choice["tool"]}+ return None++ def _build_converse_request(+ self,+ messages: List[OpenAIMessage],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> Dict[str, Any]:+ request: Dict[str, Any] = {"modelId": str(self.model_type)}+ system_blocks, bedrock_messages = (+ self._convert_openai_to_bedrock_messages(messages)+ )+ request["messages"] = bedrock_messages+ if system_blocks:+ request["system"] = system_blocks++ inference_config: Dict[str, Any] = {}+ max_tokens = self.model_config_dict.get("max_tokens")+ temperature = self.model_config_dict.get("temperature")+ top_p = self.model_config_dict.get("top_p")+ stop = self.model_config_dict.get("stop")+ if isinstance(max_tokens, int):+ inference_config["maxTokens"] = max_tokens+ if isinstance(temperature, (int, float)):+ inference_config["temperature"] = float(temperature)+ if isinstance(top_p, (int, float)):+ inference_config["topP"] = float(top_p)+ if isinstance(stop, list):+ inference_config["stopSequences"] = stop+ if inference_config:+ request["inferenceConfig"] = inference_config++ additional_fields: Dict[str, Any] = {}+ if self.model_config_dict.get("top_k") is not None:+ additional_fields["top_k"] = self.model_config_dict["top_k"]+ if self.model_config_dict.get("reasoning_effort") is not None:+ additional_fields["reasoning_effort"] = self.model_config_dict[+ "reasoning_effort"+ ]+ if additional_fields:+ request["additionalModelRequestFields"] = additional_fields++ resolved_tools = tools+ if resolved_tools is None:+ config_tools = self.model_config_dict.get("tools")+ resolved_tools = (+ config_tools if isinstance(config_tools, list) else None+ )++ if self.model_config_dict.get("tool_choice") == "none":+ resolved_tools = None++ bedrock_tools = self._convert_openai_tools_to_bedrock(resolved_tools)+ if bedrock_tools:+ tool_config: Dict[str, Any] = {"tools": bedrock_tools}+ converted_choice = self._convert_tool_choice(+ self.model_config_dict.get("tool_choice")+ )+ if converted_choice:+ tool_config["toolChoice"] = converted_choice+ request["toolConfig"] = tool_config++ return request++ @staticmethod+ def _map_stop_reason(stop_reason: Optional[str]) -> str:+ return {+ "end_turn": "stop",+ "max_tokens": "length",+ "stop_sequence": "stop",+ "tool_use": "tool_calls",+ }.get(stop_reason or "", "stop")++ @staticmethod+ def _parse_bedrock_usage(usage_raw: Dict[str, Any]) -> Dict[str, Any]:+ prompt_tokens = int(usage_raw.get("inputTokens", 0) or 0)+ completion_tokens = int(usage_raw.get("outputTokens", 0) or 0)+ cache_read = int(usage_raw.get("cacheReadInputTokens", 0) or 0)+ cache_write = int(usage_raw.get("cacheWriteInputTokens", 0) or 0)+ return {+ "prompt_tokens": prompt_tokens,+ "completion_tokens": completion_tokens,+ "total_tokens": prompt_tokens + completion_tokens,+ "prompt_tokens_details": {+ "cached_tokens": cache_read,+ "cache_write_tokens": cache_write,+ },+ }++ def _convert_converse_to_openai_response(+ self,+ response: Dict[str, Any],+ model: str,+ ) -> ChatCompletion:+ output = response.get("output", {})+ message = output.get("message", {})+ content_blocks = message.get("content", [])++ text_parts: List[str] = []+ tool_calls: List[Dict[str, Any]] = []++ for block in content_blocks:+ if not isinstance(block, dict):+ continue+ if isinstance(block.get("text"), str):+ text_parts.append(block["text"])+ continue+ tool_use = block.get("toolUse")+ if not isinstance(tool_use, dict):+ continue+ tool_id = str(tool_use.get("toolUseId", ""))+ tool_name = str(tool_use.get("name", ""))+ tool_input = tool_use.get("input", {})+ tool_calls.append(+ {+ "id": tool_id,+ "type": "function",+ "function": {+ "name": tool_name,+ "arguments": json.dumps(+ tool_input, ensure_ascii=False+ ),+ },+ }+ )++ usage = self._parse_bedrock_usage(response.get("usage", {}) or {})++ message_payload: Dict[str, Any] = {+ "role": "assistant",+ "content": "".join(text_parts),+ }+ if tool_calls:+ message_payload["tool_calls"] = tool_calls++ request_id = (+ response.get("ResponseMetadata", {}).get("RequestId")+ or f"chatcmpl-bedrock-{int(time.time())}"+ )+ finish_reason = self._map_stop_reason(response.get("stopReason"))++ return ChatCompletion.construct(+ id=request_id,+ choices=[+ {+ "index": 0,+ "message": message_payload,+ "finish_reason": finish_reason,+ }+ ],+ created=int(time.time()),+ model=model,+ object="chat.completion",+ usage=usage,+ )++ def _converse_stream_to_openai_chunks(+ self,+ stream_obj: Dict[str, Any],+ model: str,+ ) -> Generator[ChatCompletionChunk, None, None]:+ request_id = f"chatcmpl-bedrock-stream-{int(time.time())}"+ block_to_tool_idx: Dict[int, int] = {}+ next_tool_idx = 0+ finish_reason_sent = False++ def _make_chunk(**kwargs: Any) -> ChatCompletionChunk:+ return ChatCompletionChunk.construct(+ id=request_id,+ created=int(time.time()),+ model=model,+ object="chat.completion.chunk",+ **kwargs,+ )++ for event in stream_obj.get("stream", []):+ if "messageStart" in event:+ yield _make_chunk(+ choices=[{"index": 0, "delta": {}, "finish_reason": None}]+ )+ continue++ if "contentBlockStart" in event:+ start = event["contentBlockStart"].get("start", {})+ block_idx = int(+ event["contentBlockStart"].get("contentBlockIndex", 0)+ )+ tool_use = start.get("toolUse")+ if isinstance(tool_use, dict):+ block_to_tool_idx[block_idx] = next_tool_idx+ tool_id = str(tool_use.get("toolUseId", ""))+ tool_name = str(tool_use.get("name", ""))+ yield _make_chunk(+ choices=[+ {+ "index": 0,+ "delta": {+ "tool_calls": [+ {+ "index": next_tool_idx,+ "id": tool_id,+ "type": "function",+ "function": {+ "name": tool_name,+ "arguments": "",+ },+ }+ ]+ },+ "finish_reason": None,+ }+ ]+ )+ next_tool_idx += 1+ continue++ if "contentBlockDelta" in event:+ delta_obj = event["contentBlockDelta"].get("delta", {})+ block_idx = int(+ event["contentBlockDelta"].get("contentBlockIndex", 0)+ )++ if isinstance(delta_obj.get("text"), str):+ yield _make_chunk(+ choices=[+ {+ "index": 0,+ "delta": {"content": delta_obj["text"]},+ "finish_reason": None,+ }+ ]+ )+ continue++ tool_use = delta_obj.get("toolUse")+ if isinstance(tool_use, dict):+ raw_input = tool_use.get("input", "")+ argument_delta = (+ raw_input+ if isinstance(raw_input, str)+ else json.dumps(raw_input, ensure_ascii=False)+ )+ mapped_idx = block_to_tool_idx.get(block_idx, 0)+ yield _make_chunk(+ choices=[+ {+ "index": 0,+ "delta": {+ "tool_calls": [+ {+ "index": mapped_idx,+ "function": {+ "arguments": argument_delta+ },+ }+ ]+ },+ "finish_reason": None,+ }+ ]+ )+ continue++ if "metadata" in event:+ usage = self._parse_bedrock_usage(+ event["metadata"].get("usage", {}) or {}+ )+ yield _make_chunk(+ choices=[{"index": 0, "delta": {}, "finish_reason": None}],+ usage=usage,+ )+ continue++ if "messageStop" in event:+ reason = event["messageStop"].get("stopReason")+ finish_reason_sent = True+ yield _make_chunk(+ choices=[+ {+ "index": 0,+ "delta": {},+ "finish_reason": self._map_stop_reason(reason),+ }+ ]+ )++ if not finish_reason_sent:+ yield _make_chunk(+ choices=[{"index": 0, "delta": {}, "finish_reason": "stop"}]+ )++ @observe()+ def _run(+ self,+ messages: List[OpenAIMessage],+ response_format: Optional[Type[BaseModel]] = None,+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> Union[+ ChatCompletion,+ Generator[ChatCompletionChunk, None, None],+ ]:+ if response_format is not None:+ warnings.warn(+ "The 'response_format' parameter is not supported by "+ "Bedrock Converse and will be ignored.",+ UserWarning,+ stacklevel=2,+ )++ request = self._build_converse_request(messages, tools=tools)+ if self.model_config_dict.get("stream", False):+ stream_resp = self.bedrock_client.converse_stream(**request)+ return self._converse_stream_to_openai_chunks(+ stream_obj=stream_resp, model=str(self.model_type)+ )+ response = self.bedrock_client.converse(**request)+ return self._convert_converse_to_openai_response(+ response=response, model=str(self.model_type)+ )++ @observe()+ async def _arun(+ self,+ messages: List[OpenAIMessage],+ response_format: Optional[Type[BaseModel]] = None,+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> None:+ raise NotImplementedError(+ "Async inference is not supported for AWSBedrockConverseModel "+ "because boto3 Bedrock Runtime client is synchronous. "+ "Use `_run` instead."+ )diff --git a/camel/models/aws_bedrock_model.py b/camel/models/aws_bedrock_model.pyindex 57492553b..68f700d5d 100644--- a/camel/models/aws_bedrock_model.py+++ b/camel/models/aws_bedrock_model.py@@ -13,6 +13,7 @@# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========import os+import warningsfrom typing import Any, Dict, Optional, Unionfrom camel.configs import BedrockConfig@@ -72,6 +73,16 @@ class AWSBedrockModel(OpenAICompatibleModel):) -> None:if model_config_dict is None:model_config_dict = BedrockConfig().as_dict()+ # Prompt-caching controls are for AWSBedrockConverseModel. Remove them+ # to avoid passing unknown keys to the OpenAI-compatible endpoint.+ cache_control = model_config_dict.pop("cache_control", None)+ if cache_control is not None:+ warnings.warn(+ "Ignoring `cache_control` on AWSBedrockModel. "+ "Use AWSBedrockConverseModel for Bedrock prompt caching.",+ UserWarning,+ stacklevel=2,+ )api_key = api_key or os.environ.get("BEDROCK_API_KEY")url = url or os.environ.get("BEDROCK_API_BASE_URL",diff --git a/camel/models/gemini_model.py b/camel/models/gemini_model.pyindex 8a24f2fa1..de356e6b7 100644--- a/camel/models/gemini_model.py+++ b/camel/models/gemini_model.py@@ -12,7 +12,9 @@# limitations under the License.# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========import os+import refrom typing import (+ TYPE_CHECKING,Any,AsyncGenerator,Dict,@@ -21,12 +23,18 @@ from typing import (Optional,Type,Union,+ cast,)from openai import AsyncStream, Stream++if TYPE_CHECKING:+ from google.genai.client import Client as GenaiClient+ from google.genai.types import CachedContentfrom pydantic import BaseModelfrom camel.configs import GeminiConfig+from camel.logger import get_loggerfrom camel.messages import OpenAIMessagefrom camel.models.openai_compatible_model import OpenAICompatibleModelfrom camel.types import (@@ -53,6 +61,9 @@ else:from camel.utils import observe+logger = get_logger(__name__)++class GeminiModel(OpenAICompatibleModel):r"""Gemini API in a unified OpenAICompatibleModel interface.@@ -100,7 +111,13 @@ class GeminiModel(OpenAICompatibleModel):) -> None:if model_config_dict is None:model_config_dict = GeminiConfig().as_dict()++ # Extract cache parameters before passing to parent+ self._cache_control = model_config_dict.pop("cache_control", None)+ self._cached_content = model_config_dict.pop("cached_content", None)+api_key = api_key or os.environ.get("GEMINI_API_KEY")+ self._api_key = api_keyurl = url or os.environ.get("GEMINI_API_BASE_URL","https://generativelanguage.googleapis.com/v1beta/openai/",@@ -117,6 +134,258 @@ class GeminiModel(OpenAICompatibleModel):**kwargs,)+ # Lazy-initialized native client for cache operations+ self._native_client: Optional["GenaiClient"] = None++ @property+ def native_client(self) -> "GenaiClient":+ r"""Get the native google-genai client for cache operations.++ Returns:+ genai.Client: The native Google GenAI client.+ """+ if self._native_client is None:+ from google import genai++ self._native_client = genai.Client(api_key=self._api_key)+ return self._native_client++ _TTL_PATTERN = re.compile(r"^\d+s$")++ def create_cache(+ self,+ contents: List[Dict[str, Any]],+ ttl: str = "300s",+ display_name: Optional[str] = None,+ system_instruction: Optional[str] = None,+ ) -> str:+ r"""Create a cache for reusing content across requests.++ Args:+ contents (List[Dict[str, Any]]): The content to cache. Should be a+ list of message dicts with 'role' and 'parts' keys.+ ttl (str, optional): Time-to-live for the cache as an integer+ number of seconds followed by ``"s"``, e.g. ``"300s"``+ (5 min), ``"3600s"`` (1 h), ``"86400s"`` (1 day).+ (default: :obj:`"300s"`)+ display_name (str, optional): A human-readable name for the cache.+ (default: :obj:`None`)+ system_instruction (str, optional): System instruction to cache.+ (default: :obj:`None`)++ Returns:+ str: The cache name in format "cachedContents/{cache_id}".++ Raises:+ ValueError: If *ttl* does not match the ``"<number>s"`` format.+ """+ if not self._TTL_PATTERN.match(ttl):+ raise ValueError(+ f"Invalid ttl format: {ttl!r}. "+ "Expected an integer number of seconds followed by 's', "+ "e.g. '300s', '3600s'."+ )+ from google.genai import types++ # Build model name - handle both ModelType enum and string+ model_name = (+ self.model_type.value+ if hasattr(self.model_type, 'value')+ else str(self.model_type)+ )+ # Ensure model name has proper prefix+ if not model_name.startswith("models/"):+ model_name = f"models/{model_name}"++ config = types.CreateCachedContentConfig(+ contents=cast(Any, contents),+ ttl=ttl,+ display_name=display_name,+ system_instruction=system_instruction,+ )++ # google-genai expects contents inside config.+ cache = self.native_client.caches.create(+ model=model_name,+ config=config,+ )+ if not cache.name:+ raise RuntimeError("Gemini cache creation succeeded without name.")+ return cache.name++ def list_caches(self) -> List["CachedContent"]:+ r"""List all caches for the current API key.++ Returns:+ List[CachedContent]: A list of cached content objects.+ """+ return list(self.native_client.caches.list())++ def get_cache(self, cache_name: str) -> "CachedContent":+ r"""Get details of a specific cache.++ Args:+ cache_name (str): The cache name in format "cachedContents/{id}".++ Returns:+ CachedContent: The cached content object.+ """+ return self.native_client.caches.get(name=cache_name)++ def update_cache(self, cache_name: str, ttl: str) -> "CachedContent":+ r"""Update the TTL of an existing cache.++ Args:+ cache_name (str): The cache name in format "cachedContents/{id}".+ ttl (str): New time-to-live for the cache.++ Returns:+ CachedContent: The updated cached content object.+ """+ from google.genai import types++ config = types.UpdateCachedContentConfig(ttl=ttl)+ return self.native_client.caches.update(+ name=cache_name,+ config=config,+ )++ def delete_cache(self, cache_name: str) -> None:+ r"""Delete a cache.++ Args:+ cache_name (str): The cache name in format "cachedContents/{id}".+ """+ self.native_client.caches.delete(name=cache_name)++ @property+ def cached_content(self) -> Optional[str]:+ r"""Get the current cached content name.++ Returns:+ Optional[str]: The cache name or None if not set.+ """+ return self._cached_content++ @cached_content.setter+ def cached_content(self, value: Optional[str]) -> None:+ r"""Set or clear the cached content to use for requests.++ Args:+ value (Optional[str]): The cache name (e.g., "cachedContents/xxx")+ or None to clear.+ """+ self._cached_content = value++ @staticmethod+ def _sanitize_tools_for_gemini(+ tools: List[Dict[str, Any]],+ ) -> List[Dict[str, Any]]:+ import copy++ sanitized_tools = copy.deepcopy(tools)++ # Remove strict and anyOf from each tool's function parameters since+ # Gemini does not support them.+ for tool in sanitized_tools:+ function_dict = tool.get('function', {})+ function_dict.pop("strict", None)++ if 'parameters' not in function_dict:+ continue+ params = function_dict['parameters']+ if 'properties' not in params:+ continue++ for prop_name, prop_value in params['properties'].items():+ if 'anyOf' in prop_value:+ # Replace anyOf with the first type in the list.+ first_type = prop_value['anyOf'][0]+ params['properties'][prop_name] = first_type+ # Preserve description if it exists.+ if 'description' in prop_value:+ params['properties'][prop_name]['description'] = (+ prop_value['description']+ )++ # Handle enum and format restrictions for Gemini API.+ if prop_value.get('type') != 'string':+ prop_value.pop('enum', None)+ if prop_value.get('type') not in [+ 'string',+ 'integer',+ 'number',+ ]:+ prop_value.pop('format', None)++ return sanitized_tools++ def _prepare_request_config(+ self,+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> Dict[str, Any]:+ request_config = super()._prepare_request_config(+ self._sanitize_tools_for_gemini(tools) if tools else None+ )++ # Remove cache config params from base request payload.+ request_config.pop("cache_control", None)+ request_config.pop("cached_content", None)++ # `**request_config` unpacking makes the SDK flatten `extra_body`+ # keys into the request JSON root. Gemini needs a literal+ # `extra_body` field, so we double-wrap:+ # extra_body={"extra_body": {...}}+ raw_extra_body = request_config.get("extra_body")+ if isinstance(raw_extra_body, dict) and isinstance(+ raw_extra_body.get("extra_body"), dict+ ):+ gemini_extra_body = raw_extra_body["extra_body"]+ elif isinstance(raw_extra_body, dict):+ gemini_extra_body = raw_extra_body+ else:+ gemini_extra_body = {}++ if self._cached_content:+ google_body = gemini_extra_body.get("google")+ if not isinstance(google_body, dict):+ google_body = {}+ google_body["cached_content"] = self._cached_content+ gemini_extra_body["google"] = google_body++ if gemini_extra_body:+ request_config["extra_body"] = {"extra_body": gemini_extra_body}++ return request_config++ def _is_stale_cached_content_error(self, err: Exception) -> bool:+ if not self._cached_content:+ return False++ from google.genai.errors import ClientError++ if not isinstance(err, ClientError):+ return False++ # Expired / deleted cached content returns 403 PERMISSION_DENIED+ # with message "CachedContent not found (or permission denied)".+ if err.code != 403:+ return False++ return "CachedContent not found" in str(err)++ def _handle_stale_cache_error(self, err: Exception) -> None:+ r"""If *err* is a stale-cached-content error, log a warning and clear+ the cache so the caller can retry. Otherwise re-raise *err*."""+ if not self._is_stale_cached_content_error(err):+ raise+ logger.warning(+ "Gemini cached_content %s is unavailable; "+ "retrying without cache.",+ self._cached_content,+ )+ self._cached_content = None+def _process_messages(self, messages) -> List[OpenAIMessage]:r"""Process the messages for Gemini API to ensure no empty content,which is not accepted by Gemini. Also preserves thought signatures@@ -540,55 +809,24 @@ class GeminiModel(OpenAICompatibleModel):messages: List[OpenAIMessage],tools: Optional[List[Dict[str, Any]]] = None,) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:- import copy-- request_config = copy.deepcopy(self.model_config_dict)- # Remove strict and anyOf from each tool's function parameters since- # Gemini does not support them- if tools:- for tool in tools:- function_dict = tool.get('function', {})- function_dict.pop("strict", None)-- # Process parameters to remove anyOf and handle enum/format- if 'parameters' in function_dict:- params = function_dict['parameters']- if 'properties' in params:- for prop_name, prop_value in params[- 'properties'- ].items():- if 'anyOf' in prop_value:- # Replace anyOf with the first type in the list- first_type = prop_value['anyOf'][0]- params['properties'][prop_name] = first_type- # Preserve description if it exists- if 'description' in prop_value:- params['properties'][prop_name][- 'description'- ] = prop_value['description']-- # Handle enum and format restrictions for Gemini- # API enum: only allowed for string type- if prop_value.get('type') != 'string':- prop_value.pop('enum', None)-- # format: only allowed for string, integer, and- # number types- if prop_value.get('type') not in [- 'string',- 'integer',- 'number',- ]:- prop_value.pop('format', None)-- request_config["tools"] = tools-- response = self._call_client(- self._client.chat.completions.create,- messages=messages,- model=self.model_type,- **request_config,- )+ request_config = self._prepare_request_config(tools)++ try:+ response = self._call_client(+ self._client.chat.completions.create,+ messages=messages,+ model=self.model_type,+ **request_config,+ )+ except Exception as err:+ self._handle_stale_cache_error(err)+ request_config = self._prepare_request_config(tools)+ response = self._call_client(+ self._client.chat.completions.create,+ messages=messages,+ model=self.model_type,+ **request_config,+ )# Preserve thought signatures from the response for future requestsreturn self._preserve_thought_signatures(response) # type: ignore[return-value]@@ -598,55 +836,52 @@ class GeminiModel(OpenAICompatibleModel):messages: List[OpenAIMessage],tools: Optional[List[Dict[str, Any]]] = None,) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:- import copy-- request_config = copy.deepcopy(self.model_config_dict)- # Remove strict and anyOf from each tool's function parameters since- # Gemini does not support them- if tools:- for tool in tools:- function_dict = tool.get('function', {})- function_dict.pop("strict", None)-- # Process parameters to remove anyOf and handle enum/format- if 'parameters' in function_dict:- params = function_dict['parameters']- if 'properties' in params:- for prop_name, prop_value in params[- 'properties'- ].items():- if 'anyOf' in prop_value:- # Replace anyOf with the first type in the list- first_type = prop_value['anyOf'][0]- params['properties'][prop_name] = first_type- # Preserve description if it exists- if 'description' in prop_value:- params['properties'][prop_name][- 'description'- ] = prop_value['description']-- # Handle enum and format restrictions for Gemini- # API enum: only allowed for string type- if prop_value.get('type') != 'string':- prop_value.pop('enum', None)-- # format: only allowed for string, integer, and- # number types- if prop_value.get('type') not in [- 'string',- 'integer',- 'number',- ]:- prop_value.pop('format', None)-- request_config["tools"] = tools-- response = await self._acall_client(- self._async_client.chat.completions.create,- messages=messages,- model=self.model_type,- **request_config,- )+ request_config = self._prepare_request_config(tools)++ try:+ response = await self._acall_client(+ self._async_client.chat.completions.create,+ messages=messages,+ model=self.model_type,+ **request_config,+ )+ except Exception as err:+ self._handle_stale_cache_error(err)+ request_config = self._prepare_request_config(tools)+ response = await self._acall_client(+ self._async_client.chat.completions.create,+ messages=messages,+ model=self.model_type,+ **request_config,+ )# Preserve thought signatures from the response for future requestsreturn self._preserve_thought_signatures(response) # type: ignore[return-value]++ def _request_parse(+ self,+ messages: List[OpenAIMessage],+ response_format: Type[BaseModel],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> ChatCompletion:+ try:+ return super()._request_parse(messages, response_format, tools)+ except Exception as err:+ self._handle_stale_cache_error(err)+ return super()._request_parse(messages, response_format, tools)++ async def _arequest_parse(+ self,+ messages: List[OpenAIMessage],+ response_format: Type[BaseModel],+ tools: Optional[List[Dict[str, Any]]] = None,+ ) -> ChatCompletion:+ try:+ return await super()._arequest_parse(+ messages, response_format, tools+ )+ except Exception as err:+ self._handle_stale_cache_error(err)+ return await super()._arequest_parse(+ messages, response_format, tools+ )diff --git a/camel/models/model_factory.py b/camel/models/model_factory.pyindex 481e40e34..19b1306f8 100644--- a/camel/models/model_factory.py+++ b/camel/models/model_factory.py@@ -21,6 +21,7 @@ from camel.models.amd_model import AMDModelfrom camel.models.anthropic_model import AnthropicModelfrom camel.models.atlascloud_model import AtlasCloudModelfrom camel.models.avian_model import AvianModel+from camel.models.aws_bedrock_converse_model import AWSBedrockConverseModelfrom camel.models.aws_bedrock_model import AWSBedrockModelfrom camel.models.azure_openai_model import AzureOpenAIModelfrom camel.models.base_model import BaseModelBackend@@ -83,6 +84,7 @@ class ModelFactory:ModelPlatformType.TOGETHER: TogetherAIModel,ModelPlatformType.LITELLM: LiteLLMModel,ModelPlatformType.AWS_BEDROCK: AWSBedrockModel,+ ModelPlatformType.AWS_BEDROCK_CONVERSE: AWSBedrockConverseModel,ModelPlatformType.NVIDIA: NvidiaModel,ModelPlatformType.SILICONFLOW: SiliconFlowModel,ModelPlatformType.AMD: AMDModel,diff --git a/camel/types/enums.py b/camel/types/enums.pyindex 44081cd70..b992344d0 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -1877,6 +1877,7 @@ class ModelPlatformType(Enum):OPENAI = "openai"AWS_BEDROCK = "aws-bedrock"+ AWS_BEDROCK_CONVERSE = "aws-bedrock-converse"AZURE = "azure"ANTHROPIC = "anthropic"GROQ = "groq"@@ -1937,7 +1938,10 @@ class ModelPlatformType(Enum):@propertydef is_aws_bedrock(self) -> bool:r"""Returns whether this platform is aws-bedrock."""- return self is ModelPlatformType.AWS_BEDROCK+ return self in (+ ModelPlatformType.AWS_BEDROCK,+ ModelPlatformType.AWS_BEDROCK_CONVERSE,+ )@propertydef is_azure(self) -> bool:diff --git a/examples/models/anthropic_prompt_caching_example.py b/examples/models/anthropic_prompt_caching_example.pydeleted file mode 100644index 7130dba41..000000000--- a/examples/models/anthropic_prompt_caching_example.py+++ /dev/null@@ -1,154 +0,0 @@-# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========--"""-Anthropic Prompt Caching Example--This example demonstrates how to use Anthropic's prompt caching (via the-`cache_control` parameter) in CAMEL, and how it can reduce prompt token-usage and latency when you repeatedly send a large shared context.--Usage:- export ANTHROPIC_API_KEY="your-api-key-here"- python -m examples.models.anthropic_prompt_caching_example-"""--import textwrap-import time--from camel.agents import ChatAgent-from camel.configs import AnthropicConfig-from camel.models import ModelFactory-from camel.types import ModelPlatformType, ModelType---def build_long_shared_context() -> str:- r"""Build a relatively long shared context to better observe- prompt caching effects.-- In real use cases you would put a long document here, such as- a product spec, API reference, or knowledge base article.- """- base_paragraph = textwrap.dedent(- """- CAMEL-AI is an open-source framework for building autonomous,- communicative AI agents and multi-agent systems. It provides- abstractions for agents, tools, memories, environments, and- workflows, making it easy to prototype and deploy complex AI- systems in production.-- In this example we simulate a long reference document by- repeating this paragraph multiple times. This makes the prompt- large enough that Anthropic's prompt caching can have a- noticeable impact on prompt token usage and latency when the- same context is reused across multiple requests.- """- ).strip()-- # Repeat the paragraph to simulate a large document.- return "\n\n".join(f"[Section {i+1}]\n{base_paragraph}" for i in range(20))---def ask_with_agent(- agent: ChatAgent,- shared_context: str,- question: str,-) -> None:- r"""Send one question with the shared context and print usage stats."""- user_msg = f"Question: {question}"-- start_time = time.time()- response = agent.step(shared_context)- response = agent.step(user_msg)- elapsed = time.time() - start_time-- usage = response.info.get("usage", {}) or {}-- print("-" * 80)- print(f"Question: {question}")- print(f"Latency: {elapsed:.2f}s")- print(f"Usage: {usage}")- print("Assistant:", response.msgs[0].content[:300], "...\n")---def main() -> None:- # Please set the environment variable:- # export ANTHROPIC_API_KEY="your-api-key-here"-- shared_context = build_long_shared_context()- system_message = "You are a helpful assistant that provides detailed and informative responses." # noqa: E501-- # Common model config- model_config = AnthropicConfig(- temperature=0.2,- max_tokens=512,- ).as_dict()-- # Model WITHOUT prompt caching- model_no_cache = ModelFactory.create(- model_platform=ModelPlatformType.ANTHROPIC,- model_type=ModelType.CLAUDE_SONNET_4_5,- model_config_dict=model_config,- )-- # Model WITH prompt caching enabled (cache shared context for 5 minutes)- model_with_cache = ModelFactory.create(- model_platform=ModelPlatformType.ANTHROPIC,- model_type=ModelType.CLAUDE_SONNET_4_5,- model_config_dict=AnthropicConfig(- temperature=0.2,- max_tokens=512,- cache_control="5m",- ).as_dict(),- )-- agent_no_cache = ChatAgent(- system_message=system_message,- model=model_no_cache,- )- agent_with_cache = ChatAgent(- system_message=system_message,- model=model_with_cache,- )-- questions = [- "Summarize the core objectives and main functionalities of this document.", # noqa: E501- "List three typical use cases for CAMEL-AI based on the document content.", # noqa: E501- "What are the things I need to pay attention to if I want to deploy a multi-agent system in a production environment based on this document?", # noqa: E501- ]-- print("=" * 80)- print("Anthropic Prompt Caching Demo (WITHOUT cache_control)")- print("=" * 80)- for q in questions:- agent_no_cache.reset()- ask_with_agent(agent_no_cache, shared_context, q)-- print("\n" + "=" * 80)- print("Anthropic Prompt Caching Demo (WITH cache_control='5m')")- print("=" * 80)- for q in questions:- agent_with_cache.reset()- ask_with_agent(agent_with_cache, shared_context, q)-- print(- "\nExplanation:\n"- "- The first group of calls does not enable prompt caching, so each time the full length of the document's prompt tokens is counted.\n" # noqa: E501- "- The second group of calls enables Anthropic's prompt caching by `cache_control='5m'`.\n" # noqa: E501- " In the case of repeated use of the same long document, you should see that the `prompt_tokens` and latency (Latency) of the latter requests are significantly lower than the first group.\n" # noqa: E501- )---if __name__ == "__main__":- main()diff --git a/examples/models/prompt_caching_anthropic_example.py b/examples/models/prompt_caching_anthropic_example.pynew file mode 100644index 000000000..bfbdba31e--- /dev/null+++ b/examples/models/prompt_caching_anthropic_example.py@@ -0,0 +1,94 @@+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========++"""+Anthropic prompt caching example — let the agent fetch and analyse a blog.++The agent is given a ``fetch_url`` tool so it can retrieve a web page on its+own. We then ask follow-up questions about the same article. With+``cache_control="5m"`` the long conversation context is cached after the+first request, making follow-up questions faster and cheaper.++Required environment variable:+ export ANTHROPIC_API_KEY="sk-..."+"""++import httpx++from camel.agents import ChatAgent+from camel.configs import AnthropicConfig+from camel.models import ModelFactory+from camel.toolkits import FunctionTool+from camel.types import ModelPlatformType, ModelType++BLOG_URL = (+ "https://www.camel-ai.org/blogs/"+ "seta-scaling-environments-for-terminal-agents"+)+++def fetch_url(url: str) -> str:+ """Fetch the text content of a web page.++ Args:+ url: The URL to fetch.++ Returns:+ The page content as plain text.+ """+ resp = httpx.get(url, follow_redirects=True, timeout=30)+ resp.raise_for_status()+ return resp.text+++# ── Create model with prompt caching ──────────────────────────────────────++model = ModelFactory.create(+ model_platform=ModelPlatformType.ANTHROPIC,+ model_type=ModelType.CLAUDE_SONNET_4_5,+ model_config_dict=AnthropicConfig(+ max_tokens=64000,+ cache_control="5m",+ ).as_dict(),+)++agent = ChatAgent(+ system_message="You are a helpful assistant.",+ model=model,+ tools=[FunctionTool(fetch_url)],+)++# ── First question: agent fetches the article itself ──────────────────────++response = agent.step(+ f"Please read this blog post and summarise it in 2-3 sentences: {BLOG_URL}"+)+print("[Question 1] Summarise the blog post.")+print(f" Usage: {response.info.get('usage', {})}")+print(f" Answer: {response.msgs[0].content[:200]}...")+print()++# ── Follow-up questions reuse the cached context ─────────────────────────++follow_ups = [+ "What success rate did Claude Sonnet 4.5 achieve on Terminal-Bench 2.0?",+ "What are the main failure categories mentioned in the article?",+]++for i, question in enumerate(follow_ups, 2):+ response = agent.step(question)+ print(f"[Question {i}] {question}")+ print(f" Usage: {response.info.get('usage', {})}")+ print(f" Answer: {response.msgs[0].content[:200]}...")+ print()diff --git a/examples/models/prompt_caching_azure_example.py b/examples/models/prompt_caching_azure_example.pynew file mode 100644index 000000000..b4d516b07--- /dev/null+++ b/examples/models/prompt_caching_azure_example.py@@ -0,0 +1,99 @@+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========++"""+Azure OpenAI prompt caching example — let the agent fetch and analyse a blog.++Azure OpenAI caching is automatic for GPT-4o+ models with 1024+ tokens.++Required environment variables:+ export AZURE_OPENAI_API_KEY="..."+ export AZURE_OPENAI_BASE_URL="https://your-resource.openai.azure.com/"+ export AZURE_DEPLOYMENT_NAME="gpt-4o" # optional, defaults to gpt-4o+ export AZURE_API_VERSION="2024-12-01-preview" # optional+"""++import os++import httpx++from camel.agents import ChatAgent+from camel.configs import ChatGPTConfig+from camel.models import ModelFactory+from camel.toolkits import FunctionTool+from camel.types import ModelPlatformType++BLOG_URL = (+ "https://www.camel-ai.org/blogs/"+ "seta-scaling-environments-for-terminal-agents"+)+++def fetch_url(url: str) -> str:+ """Fetch the text content of a web page.++ Args:+ url: The URL to fetch.++ Returns:+ The page content as plain text.+ """+ resp = httpx.get(url, follow_redirects=True, timeout=30)+ resp.raise_for_status()+ return resp.text+++# ── Create model ───────────────────────────────────────────────────────────++deployment_name = os.getenv("AZURE_DEPLOYMENT_NAME", "gpt-4o")+api_version = os.getenv("AZURE_API_VERSION", "2024-12-01-preview")++model = ModelFactory.create(+ model_platform=ModelPlatformType.AZURE,+ model_type=deployment_name,+ api_version=api_version,+ model_config_dict=ChatGPTConfig(+ prompt_cache_key="blog_analysis_cache", # optional+ ).as_dict(),+)++agent = ChatAgent(+ system_message="You are a helpful assistant.",+ model=model,+ tools=[FunctionTool(fetch_url)],+)++# ── First question: agent fetches the article itself ──────────────────────++response = agent.step(+ f"Please read this blog post and summarise it in 2-3 sentences: {BLOG_URL}"+)+print("[Question 1] Summarise the blog post.")+print(f" Usage: {response.info.get('usage', {})}")+print(f" Answer: {response.msgs[0].content[:200]}...")+print()++# ── Follow-up questions — caching kicks in automatically ─────────────────++follow_ups = [+ "What success rate did Claude Sonnet 4.5 achieve on Terminal-Bench 2.0?",+ "What are the main failure categories mentioned in the article?",+]++for i, question in enumerate(follow_ups, 2):+ response = agent.step(question)+ print(f"[Question {i}] {question}")+ print(f" Usage: {response.info.get('usage', {})}")+ print(f" Answer: {response.msgs[0].content[:200]}...")+ print()diff --git a/examples/models/prompt_caching_bedrock_example.py b/examples/models/prompt_caching_bedrock_example.pynew file mode 100644index 000000000..a597c6ae3--- /dev/null+++ b/examples/models/prompt_caching_bedrock_example.py@@ -0,0 +1,96 @@+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========++"""+AWS Bedrock Converse prompt caching example — let the agent fetch and+analyse a blog.++AWSBedrockConverseModel supports implicit prompt caching via the Converse API.+Set ``cache_control`` in BedrockConfig to enable it.++Required environment variables:+ export AWS_REGION="us-east-1"+ export AWS_ACCESS_KEY_ID="..."+ export AWS_SECRET_ACCESS_KEY="..."+ # Or use bearer token auth:+ export BEDROCK_API_KEY="..."+"""++import httpx++from camel.agents import ChatAgent+from camel.configs import BedrockConfig+from camel.models import ModelFactory+from camel.toolkits import FunctionTool+from camel.types import ModelPlatformType++BLOG_URL = (+ "https://www.camel-ai.org/blogs/"+ "seta-scaling-environments-for-terminal-agents"+)+++def fetch_url(url: str) -> str:+ """Fetch the text content of a web page.++ Args:+ url: The URL to fetch.++ Returns:+ The page content as plain text.+ """+ resp = httpx.get(url, follow_redirects=True, timeout=30)+ resp.raise_for_status()+ return resp.text+++# ── Create model with prompt caching ──────────────────────────────────────++model = ModelFactory.create(+ model_platform=ModelPlatformType.AWS_BEDROCK_CONVERSE,+ model_type="us.anthropic.claude-sonnet-4-5-20250929-v1:0",+ model_config_dict=BedrockConfig(+ cache_control="5m",+ ).as_dict(),+)++agent = ChatAgent(+ system_message="You are a helpful assistant.",+ model=model,+ tools=[FunctionTool(fetch_url)],+)++# ── First question: agent fetches the article itself ──────────────────────++response = agent.step(+ f"Please read this blog post and summarise it in 2-3 sentences: {BLOG_URL}"+)+print("[Question 1] Summarise the blog post.")+print(f" Usage: {response.info.get('usage', {})}")+print(f" Answer: {response.msgs[0].content[:200]}...")+print()++# ── Follow-up questions reuse the cached context ─────────────────────────++follow_ups = [+ "What success rate did Claude Sonnet 4.5 achieve on Terminal-Bench 2.0?",+ "What are the main failure categories mentioned in the article?",+]++for i, question in enumerate(follow_ups, 2):+ response = agent.step(question)+ print(f"[Question {i}] {question}")+ print(f" Usage: {response.info.get('usage', {})}")+ print(f" Answer: {response.msgs[0].content[:200]}...")+ print()diff --git a/examples/models/prompt_caching_gemini_example.py b/examples/models/prompt_caching_gemini_example.pynew file mode 100644index 000000000..092bcaded--- /dev/null+++ b/examples/models/prompt_caching_gemini_example.py@@ -0,0 +1,98 @@+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========++"""+Gemini explicit context caching example — let the agent browse and analyse+a blog.++Unlike Anthropic/OpenAI's implicit caching, Gemini requires you to explicitly+create a cache, then reference it in requests via `cached_content`.++ModelFactory.create() returns a GeminiModel, so you can call cache management+methods (create_cache, list_caches, get_cache, update_cache, delete_cache)+directly on the model instance.++Required environment variable:+ export GEMINI_API_KEY="..."+"""++import httpx++from camel.agents import ChatAgent+from camel.configs import GeminiConfig+from camel.models import ModelFactory+from camel.types import ModelPlatformType, ModelType++BLOG_URL = (+ "https://www.camel-ai.org/blogs/"+ "seta-scaling-environments-for-terminal-agents"+)++# ── Fetch the blog content for explicit caching ───────────────────────────+# Gemini explicit caching requires creating a cache object upfront with the+# content in Gemini's native format, so we fetch the page first.++print(f"Fetching blog post from {BLOG_URL} ...")+resp = httpx.get(BLOG_URL, follow_redirects=True, timeout=30)+resp.raise_for_status()+blog_text = resp.text+print(f"Fetched {len(blog_text)} characters.\n")++# ── Create model via ModelFactory ──────────────────────────────────────────++model = ModelFactory.create(+ model_platform=ModelPlatformType.GEMINI,+ model_type=ModelType.GEMINI_3_FLASH,+ model_config_dict=GeminiConfig(+ temperature=0.2,+ ).as_dict(),+)++# ── Create a cache with the blog content ───────────────────────────────────++cache_name = model.create_cache(+ contents=[{"role": "user", "parts": [{"text": blog_text}]}],+ ttl="300s",+ display_name="seta-blog-cache",+ system_instruction="You are a helpful assistant. Answer on the article.",+)+print(f"Cache created: {cache_name}\n")++# ── Ask multiple questions using the cache ─────────────────────────────────+# When cached_content is set, Gemini rejects requests that also send+# system_instruction, so pass system_message=None.++model.cached_content = cache_name+agent = ChatAgent(system_message=None, model=model)++questions = [+ "Summarise the SETA blog post in 2-3 sentences.",+ "What success rate did Claude Sonnet 4.5 achieve on Terminal-Bench 2.0?",+ "What are the main failure categories mentioned in the article?",+]++for i, question in enumerate(questions, 1):+ agent.reset()+ response = agent.step(question)+ usage = response.info.get("usage", {})+ print(f"[Question {i}] {question}")+ print(f" Usage: {usage}")+ print(f" Answer: {response.msgs[0].content[:200]}...")+ print()++# ── Clean up ───────────────────────────────────────────────────────────────++model.delete_cache(cache_name)+model.cached_content = None+print(f"Cache deleted: {cache_name}")diff --git a/examples/models/prompt_caching_openai_example.py b/examples/models/prompt_caching_openai_example.pynew file mode 100644index 000000000..ebced186f--- /dev/null+++ b/examples/models/prompt_caching_openai_example.py@@ -0,0 +1,92 @@+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========++"""+OpenAI prompt caching example — let the agent fetch and analyse a blog.++OpenAI caching is automatic for prompts with 1024+ tokens. The agent uses+a ``fetch_url`` tool to retrieve a web page, then answers follow-up questions.+Cached tokens appear in usage on subsequent requests.++Required environment variable:+ export OPENAI_API_KEY="sk-..."+"""++import httpx++from camel.agents import ChatAgent+from camel.configs import ChatGPTConfig+from camel.models import ModelFactory+from camel.toolkits import FunctionTool+from camel.types import ModelPlatformType, ModelType++BLOG_URL = (+ "https://www.camel-ai.org/blogs/"+ "seta-scaling-environments-for-terminal-agents"+)+++def fetch_url(url: str) -> str:+ """Fetch the text content of a web page.++ Args:+ url: The URL to fetch.++ Returns:+ The page content as plain text.+ """+ resp = httpx.get(url, follow_redirects=True, timeout=30)+ resp.raise_for_status()+ return resp.text+++# ── Create model ───────────────────────────────────────────────────────────++model = ModelFactory.create(+ model_platform=ModelPlatformType.OPENAI,+ model_type=ModelType.GPT_5,+ model_config_dict=ChatGPTConfig(+ prompt_cache_key="blog_analysis_cache", # optional+ ).as_dict(),+)++agent = ChatAgent(+ system_message="You are a helpful assistant.",+ model=model,+ tools=[FunctionTool(fetch_url)],+)++# ── First question: agent fetches the article itself ──────────────────────++response = agent.step(+ f"Please read this blog post and summarise it in 2-3 sentences: {BLOG_URL}"+)+print("[Question 1] Summarise the blog post.")+print(f" Usage: {response.info.get('usage', {})}")+print(f" Answer: {response.msgs[0].content[:200]}...")+print()++# ── Follow-up questions — caching kicks in automatically ─────────────────++follow_ups = [+ "What success rate did Claude Sonnet 4.5 achieve on Terminal-Bench 2.0?",+ "What are the main failure categories mentioned in the article?",+]++for i, question in enumerate(follow_ups, 2):+ response = agent.step(question)+ print(f"[Question {i}] {question}")+ print(f" Usage: {response.info.get('usage', {})}")+ print(f" Answer: {response.msgs[0].content[:200]}...")+ print()diff --git a/pyproject.toml b/pyproject.tomlindex c26f2381f..33d7b699e 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -199,6 +199,7 @@ model_platforms = ["cohere>=5.11.0,<6","fish-audio-sdk>=1.0.0","ibm-watsonx-ai>=1.3.11",+ "boto3>=1.35.3,<2",]huggingface = ["transformers>=4,<5",@@ -218,7 +219,7 @@ storage = ["redis>=5.0.6,<6","azure-storage-blob>=12.21.0,<13","google-cloud-storage>=2.18.0,<3",- "botocore>=1.35.3,<2",+ "boto3>=1.35.3,<2","mem0ai>=0.1.73","pyobvector>=0.2.22; python_version < '3.13'","weaviate-client>=4.15.0",@@ -397,7 +398,7 @@ all = ["redis>=5.0.6,<6","azure-storage-blob>=12.21.0,<13","google-cloud-storage>=2.18.0,<3",- "botocore>=1.35.3,<2",+ "boto3>=1.35.3,<2","dappier>=0.3.3,<0.4","mypy>=1.5.1,<2","pre-commit>=3,<4",@@ -634,7 +635,8 @@ module = ["fish-audio-sdk","reka-api","agentops",- "botocore.*",+ "boto3",+ "boto3.*","arxiv","arxiv2text","praw",diff --git a/test/models/test_aws_bedrock_converse_model.py b/test/models/test_aws_bedrock_converse_model.pynew file mode 100644index 000000000..35e49f184--- /dev/null+++ b/test/models/test_aws_bedrock_converse_model.py@@ -0,0 +1,224 @@+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========++import base64+import sys+import types++import pytest++from camel.configs import BedrockConfig+from camel.models import AWSBedrockConverseModel+from camel.types import ModelType+++def _make_model(+ model_config_dict=None,+ **kwargs,+) -> AWSBedrockConverseModel:+ if model_config_dict is None:+ model_config_dict = BedrockConfig().as_dict()+ return AWSBedrockConverseModel(+ ModelType.AWS_CLAUDE_3_HAIKU,+ model_config_dict=model_config_dict,+ api_key="dummy_key",+ region_name="us-west-2",+ **kwargs,+ )++[email protected]_backend+def test_converse_cache_checkpoints_not_shared_object():+ model = _make_model(+ BedrockConfig(+ cache_control="5m",+ ).as_dict(),+ bedrock_client=object(),+ )++ system, messages = model._convert_openai_to_bedrock_messages(+ [+ {"role": "system", "content": "sys"},+ {"role": "user", "content": "hello"},+ ]+ )+ assert system[-1] == {"cachePoint": {"type": "default"}}+ assert messages[-1]["content"][-1] == {"cachePoint": {"type": "default"}}+ assert system[-1] is not messages[-1]["content"][-1]++[email protected]_backend+def test_converse_builds_tool_config_and_tool_messages():+ model = _make_model(+ BedrockConfig(+ cache_control="5m",+ tool_choice="auto",+ ).as_dict(),+ bedrock_client=object(),+ )++ request = model._build_converse_request(+ messages=[+ {"role": "system", "content": "sys"},+ {"role": "user", "content": "question"},+ {+ "role": "assistant",+ "content": "",+ "tool_calls": [+ {+ "id": "call_1",+ "type": "function",+ "function": {+ "name": "search_docs",+ "arguments": '{"q":"camel"}',+ },+ }+ ],+ },+ {+ "role": "tool",+ "tool_call_id": "call_1",+ "content": '{"answer":"ok"}',+ },+ ],+ tools=[+ {+ "type": "function",+ "function": {+ "name": "search_docs",+ "description": "Search docs",+ "parameters": {+ "type": "object",+ "properties": {"q": {"type": "string"}},+ "required": ["q"],+ },+ },+ }+ ],+ )++ assert request["toolConfig"]["toolChoice"] == {"auto": {}}+ assert (+ request["toolConfig"]["tools"][0]["toolSpec"]["name"] == "search_docs"+ )+ assert request["messages"][1]["role"] == "assistant"+ assert any(+ isinstance(block, dict) and "toolUse" in block+ for block in request["messages"][1]["content"]+ )+ assert request["messages"][2]["role"] == "user"+ assert "toolResult" in request["messages"][2]["content"][0]++[email protected]_backend+def test_converse_tool_choice_none_disables_tools():+ model = _make_model(+ BedrockConfig(tool_choice="none").as_dict(),+ bedrock_client=object(),+ )+ request = model._build_converse_request(+ messages=[{"role": "user", "content": "hello"}],+ tools=[+ {+ "type": "function",+ "function": {+ "name": "search_docs",+ "parameters": {"type": "object"},+ },+ }+ ],+ )+ assert "toolConfig" not in request++[email protected]_backend+def test_converse_supports_data_url_image_conversion():+ image_bytes = b"test-image"+ data_url = (+ "data:image/png;base64," + base64.b64encode(image_bytes).decode()+ )+ model = _make_model(bedrock_client=object())++ blocks = model._to_bedrock_content_blocks(+ [{"type": "image_url", "image_url": {"url": data_url}}]+ )+ assert blocks[0]["image"]["format"] == "png"+ assert blocks[0]["image"]["source"]["bytes"] == image_bytes++[email protected]_backend+def test_parse_json_or_text_scalar_json_is_text():+ model = _make_model(bedrock_client=object())+ assert model._parse_json_or_text("42") == {"text": "42"}+ assert model._parse_json_or_text("true") == {"text": "true"}++[email protected]_backend+def test_converse_stream_is_supported():+ class DummyEventStream:+ def __init__(self, events):+ self._events = events++ def __iter__(self):+ return iter(self._events)++ class DummyClient:+ def converse_stream(self, **kwargs):+ return {+ "stream": DummyEventStream(+ [+ {"messageStart": {"role": "assistant"}},+ {"contentBlockDelta": {"delta": {"text": "hello"}}},+ {"messageStop": {"stopReason": "end_turn"}},+ ]+ )+ }++ model = _make_model(+ BedrockConfig(stream=True).as_dict(),+ bedrock_client=DummyClient(),+ )+ chunks = list(model._run([{"role": "user", "content": "hi"}]))+ assert len(chunks) >= 2+ assert chunks[-1].choices[0].finish_reason == "stop"++[email protected]_backend+async def test_converse_async_not_supported():+ model = _make_model(bedrock_client=object())+ with pytest.raises(NotImplementedError):+ await model._arun([{"role": "user", "content": "hi"}])++[email protected]_backend+def test_bedrock_client_supports_api_key_and_region_name(monkeypatch):+ def _fake_boto3_client(service_name, **kwargs):+ return {"service_name": service_name, "kwargs": kwargs}++ fake_boto3 = types.SimpleNamespace(client=_fake_boto3_client)+ monkeypatch.setitem(sys.modules, "boto3", fake_boto3)+ monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False)++ model = AWSBedrockConverseModel(+ ModelType.AWS_CLAUDE_3_HAIKU,+ model_config_dict=BedrockConfig().as_dict(),+ api_key="AKIA_TEST_KEY",+ region_name="us-east-1",+ )++ client = model.bedrock_client+ assert client["service_name"] == "bedrock-runtime"+ assert client["kwargs"]["region_name"] == "us-east-1"+ assert "aws_access_key_id" not in client["kwargs"]diff --git a/test/models/test_aws_bedrock_model.py b/test/models/test_aws_bedrock_model.pyindex 8d0b617b2..e3fc70043 100644--- a/test/models/test_aws_bedrock_model.py+++ b/test/models/test_aws_bedrock_model.py@@ -43,7 +43,9 @@ def test_aws_bedrock_model(model_type: ModelType):url="http://dummy.url",)assert model.model_type == model_type- assert model.model_config_dict == BedrockConfig().as_dict()+ expected_config = BedrockConfig().as_dict()+ expected_config.pop("cache_control", None)+ assert model.model_config_dict == expected_configassert isinstance(model.token_counter, OpenAITokenCounter)assert isinstance(model.model_type.value_for_tiktoken, str)assert isinstance(model.model_type.token_limit, int)@@ -71,3 +73,15 @@ async def test_aws_bedrock_async_supported():# and fail with a connection error (not NotImplementedError)with pytest.raises(APIConnectionError):await model._arun([{"role": "user", "content": "Test message"}])++[email protected]_backend+def test_bedrock_config_accepts_prompt_cache_params():+ config = BedrockConfig(+ temperature=0.3,+ cache_control="5m",+ )+ config_dict = config.as_dict()++ assert config_dict["temperature"] == 0.3+ assert config_dict["cache_control"] == "5m"diff --git a/test/models/test_gemini_model.py b/test/models/test_gemini_model.pyindex 5013f0f1f..2fedb6cf6 100644--- a/test/models/test_gemini_model.py+++ b/test/models/test_gemini_model.py@@ -13,6 +13,8 @@# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========import pytest+from google.genai.errors import ClientError+from pydantic import BaseModelfrom camel.configs import GeminiConfigfrom camel.models import GeminiModel@@ -20,6 +22,20 @@ from camel.types import ModelTypefrom camel.utils import OpenAITokenCounter+def _make_stale_cache_error() -> ClientError:+ """Create a ClientError that mimics a deleted/expired cached content."""+ return ClientError(+ code=403,+ response_json={+ "error": {+ "code": 403,+ "message": "CachedContent not found (or permission denied)",+ "status": "PERMISSION_DENIED",+ }+ },+ )++@pytest.mark.model_backend@pytest.mark.parametrize("model_type",@@ -175,3 +191,274 @@ def test_gemini_process_messages_single_tool_call_unchanged():assert assistant_msg['role'] == 'assistant'assert len(assistant_msg['tool_calls']) == 1assert 'extra_content' in assistant_msg['tool_calls'][0]++[email protected]_backend+def test_gemini_config_accepts_cache_parameters():+ r"""Test that GeminiConfig accepts cache_control and cached_content+ parameters.+ """+ config = GeminiConfig(+ temperature=0.5,+ cache_control="300s",+ cached_content="cachedContents/abc123",+ )+ config_dict = config.as_dict()++ assert config_dict["cache_control"] == "300s"+ assert config_dict["cached_content"] == "cachedContents/abc123"+ assert config_dict["temperature"] == 0.5++[email protected]_backend+def test_gemini_model_extracts_cache_params():+ r"""Test that GeminiModel extracts cache params from config and stores+ them.+ """+ model_config_dict = GeminiConfig(+ temperature=0.5,+ cache_control="300s",+ cached_content="cachedContents/abc123",+ ).as_dict()++ model = GeminiModel(+ model_type=ModelType.GEMINI_2_0_FLASH,+ model_config_dict=model_config_dict,+ )++ # Cache params should be extracted and stored+ assert model._cache_control == "300s"+ assert model._cached_content == "cachedContents/abc123"++ # Cache params should not be in model_config_dict (passed to API)+ assert "cache_control" not in model.model_config_dict+ assert "cached_content" not in model.model_config_dict++ # Other params should remain+ assert model.model_config_dict["temperature"] == 0.5++[email protected]_backend+def test_gemini_model_has_cache_management_methods():+ r"""Test that GeminiModel has cache management methods."""+ model_config_dict = GeminiConfig().as_dict()+ model = GeminiModel(+ model_type=ModelType.GEMINI_2_0_FLASH,+ model_config_dict=model_config_dict,+ )++ # Verify cache management methods exist+ assert hasattr(model, 'create_cache')+ assert hasattr(model, 'list_caches')+ assert hasattr(model, 'get_cache')+ assert hasattr(model, 'update_cache')+ assert hasattr(model, 'delete_cache')+ assert hasattr(model, 'native_client')++ # Verify methods are callable+ assert callable(model.create_cache)+ assert callable(model.list_caches)+ assert callable(model.get_cache)+ assert callable(model.update_cache)+ assert callable(model.delete_cache)++[email protected]_backend+def test_gemini_model_cached_content_property():+ r"""Test that cached_content property can be set and cleared at runtime."""+ model_config_dict = GeminiConfig(+ temperature=0.5,+ cached_content="cachedContents/initial",+ ).as_dict()++ model = GeminiModel(+ model_type=ModelType.GEMINI_2_0_FLASH,+ model_config_dict=model_config_dict,+ )++ # Initial cache should be extracted from config+ assert model.cached_content == "cachedContents/initial"+ assert "cached_content" not in model.model_config_dict++ # Can change cache at runtime via property+ model.cached_content = "cachedContents/updated"+ assert model.cached_content == "cachedContents/updated"++ # Can clear cache+ model.cached_content = None+ assert model.cached_content is None++[email protected]_backend+def test_gemini_cached_content_sent_via_nested_extra_body():+ r"""Test cached_content is injected under extra_body.google."""+ model = GeminiModel(+ model_type=ModelType.GEMINI_2_0_FLASH,+ model_config_dict=GeminiConfig(+ cached_content="cachedContents/test-cache",+ ).as_dict(),+ )++ class DummyCompletions:+ def __init__(self):+ self.calls = []++ def create(self, **kwargs):+ self.calls.append(kwargs)+ return {"id": "ok"}++ dummy_completions = DummyCompletions()+ model._client = type( # type: ignore[assignment]+ "DummyClient",+ (),+ {+ "chat": type(+ "DummyChat",+ (),+ {"completions": dummy_completions},+ )()+ },+ )()++ model._request_chat_completion(+ messages=[{"role": "user", "content": "hi"}]+ )++ assert len(dummy_completions.calls) == 1+ assert (+ dummy_completions.calls[0]["extra_body"]["extra_body"]["google"][+ "cached_content"+ ]+ == "cachedContents/test-cache"+ )++[email protected]_backend+def test_gemini_cached_content_retries_without_cache_on_stale_error():+ r"""Test stale cached_content triggers a single retry without cache."""+ model = GeminiModel(+ model_type=ModelType.GEMINI_2_0_FLASH,+ model_config_dict=GeminiConfig(+ cached_content="cachedContents/stale-cache",+ ).as_dict(),+ )++ class RetryCompletions:+ def __init__(self):+ self.calls = []++ def create(self, **kwargs):+ self.calls.append(kwargs)+ if len(self.calls) == 1:+ raise _make_stale_cache_error()+ return {"id": "ok"}++ retry_completions = RetryCompletions()+ model._client = type( # type: ignore[assignment]+ "DummyClient",+ (),+ {+ "chat": type(+ "DummyChat",+ (),+ {"completions": retry_completions},+ )()+ },+ )()++ model._request_chat_completion(+ messages=[{"role": "user", "content": "hi"}]+ )++ assert len(retry_completions.calls) == 2+ assert (+ retry_completions.calls[0]["extra_body"]["extra_body"]["google"][+ "cached_content"+ ]+ == "cachedContents/stale-cache"+ )+ assert "extra_body" not in retry_completions.calls[1]+ assert model.cached_content is None++[email protected]_backend+def test_gemini_parse_path_applies_cached_content_and_retry():+ r"""Test parse path uses cache field and retries on stale cache."""+ model = GeminiModel(+ model_type=ModelType.GEMINI_2_0_FLASH,+ model_config_dict=GeminiConfig(+ cached_content="cachedContents/stale-cache",+ ).as_dict(),+ )++ class DummySchema(BaseModel):+ value: str++ class RetryParseCompletions:+ def __init__(self):+ self.calls = []++ def parse(self, **kwargs):+ self.calls.append(kwargs)+ if len(self.calls) == 1:+ raise _make_stale_cache_error()+ return {"id": "ok"}++ retry_parse_completions = RetryParseCompletions()+ model._client = type( # type: ignore[assignment]+ "DummyClient",+ (),+ {+ "beta": type(+ "DummyBeta",+ (),+ {+ "chat": type(+ "DummyChat",+ (),+ {"completions": retry_parse_completions},+ )()+ },+ )()+ },+ )()++ model._request_parse(+ messages=[{"role": "user", "content": "hi"}],+ response_format=DummySchema,+ )++ assert len(retry_parse_completions.calls) == 2+ assert (+ retry_parse_completions.calls[0]["extra_body"]["extra_body"]["google"][+ "cached_content"+ ]+ == "cachedContents/stale-cache"+ )+ assert "extra_body" not in retry_parse_completions.calls[1]+ assert model.cached_content is None++[email protected]_backend+def test_gemini_cached_content_merges_with_existing_extra_body():+ r"""Test cached_content merges with existing nested extra_body fields."""+ model = GeminiModel(+ model_type=ModelType.GEMINI_2_0_FLASH,+ model_config_dict={+ "temperature": 0.2,+ "extra_body": {+ "extra_body": {+ "google": {"thinking_config": {"type": "enabled"}}+ }+ },+ "cached_content": "cachedContents/merge-cache",+ },+ )++ request_config = model._prepare_request_config()+ assert request_config["extra_body"]["extra_body"]["google"][+ "thinking_config"+ ] == {"type": "enabled"}+ assert request_config["extra_body"]["extra_body"]["google"][+ "cached_content"+ ] == ("cachedContents/merge-cache")diff --git a/uv.lock b/uv.lockindex ceed125c1..0cbd0efae 100644--- a/uv.lock+++ b/uv.lock@@ -759,18 +759,32 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/10/cb/f2ad4230dc2eb1a74edf38f1a38b9b52277f75bef262d8908e60d957e13c/blinker-1.9.0-py3-none-any.whl", hash = "sha256:ba0efaa9080b619ff2f3459d1d500c57bddea4a6b424b60a91141db6fd2f08bc", size = 8458, upload-time = "2024-11-08T17:25:46.184Z" },]+[[package]]+name = "boto3"+version = "1.42.60"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "botocore" },+ { name = "jmespath" },+ { name = "s3transfer" },+]+sdist = { url = "https://files.pythonhosted.org/packages/b8/e0/071e00265d3d8127b28c27ba3918ba283f49b39943864a389ac3f5096ef3/boto3-1.42.60.tar.gz", hash = "sha256:3d549d15c821dcc871a0821319049e7d493ae3317121eb01e4b1f5230c19d5d4", size = 112786, upload-time = "2026-03-03T21:21:07.199Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/ed/30/156ff2b5afb7dd03383b5f97d0e32535e9c0e783917380c476fe2fbc1874/boto3-1.42.60-py3-none-any.whl", hash = "sha256:c0cc3d93cd76c99461f6e109e04bb020defe3ffcd04c6163c72836dff5591614", size = 140554, upload-time = "2026-03-03T21:21:05.194Z" },+]+[[package]]name = "botocore"-version = "1.42.59"+version = "1.42.60"source = { registry = "https://pypi.org/simple" }dependencies = [{ name = "jmespath" },{ name = "python-dateutil" },{ name = "urllib3" },]-sdist = { url = "https://files.pythonhosted.org/packages/45/ae/50fb33bdf1911c216d50f98d989dd032a506f054cf829ebd737c6fa7e3e6/botocore-1.42.59.tar.gz", hash = "sha256:5314f19e1da8fc0ebc41bdb8bbe17c9a7397d87f4d887076ac8bdef972a34138", size = 14950271, upload-time = "2026-02-27T20:25:20.614Z" }+sdist = { url = "https://files.pythonhosted.org/packages/82/d7/bfe8413cc7dc167e04ca9c68ea136251307960f662ec5889512615565b25/botocore-1.42.60.tar.gz", hash = "sha256:de9278810fb2e92a9ffe3dc8ffa68f1066e6d2caf19da9460760743b39ca5215", size = 14950855, upload-time = "2026-03-03T21:20:51.529Z" }wheels = [- { url = "https://files.pythonhosted.org/packages/59/df/9d52819e0d804ead073d53ab1823bc0f0cb172a250fba31107b0b43fbb04/botocore-1.42.59-py3-none-any.whl", hash = "sha256:d2f2ff7ecc31e86ef46b5daee112cfbca052c13801285fb23af909f7bff5b657", size = 14619293, upload-time = "2026-02-27T20:25:17.455Z" },+ { url = "https://files.pythonhosted.org/packages/8d/63/5cf970a00e9ddcbb9e65ecc79276717a9555a77d3d0571bd962676e19c3b/botocore-1.42.60-py3-none-any.whl", hash = "sha256:d8b4aab06cc134e21d294c068cb94e0eeb59bacd27c836fb6b882b61433df2f4", size = 14621726, upload-time = "2026-03-03T21:20:47.935Z" },][[package]]@@ -832,7 +846,7 @@ all = [{ name = "azure-identity" },{ name = "azure-storage-blob" },{ name = "beautifulsoup4" },- { name = "botocore" },+ { name = "boto3" },{ name = "chromadb" },{ name = "chunkr-ai" },{ name = "cohere" },@@ -1137,6 +1151,7 @@ media-tools = []model-platforms = [{ name = "anthropic" },+ { name = "boto3" },{ name = "cohere" },{ name = "fish-audio-sdk" },{ name = "ibm-watsonx-ai", version = "1.3.42", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },@@ -1227,7 +1242,7 @@ research-tools = []storage = [{ name = "azure-storage-blob" },- { name = "botocore" },+ { name = "boto3" },{ name = "chromadb" },{ name = "duckdb" },{ name = "faiss-cpu" },@@ -1305,8 +1320,9 @@ requires-dist = [{ name = "beautifulsoup4", marker = "extra == 'document-tools'", specifier = ">=4,<5" },{ name = "beautifulsoup4", marker = "extra == 'owl'", specifier = ">=4,<5" },{ name = "beautifulsoup4", marker = "extra == 'web-tools'", specifier = ">=4,<5" },- { name = "botocore", marker = "extra == 'all'", specifier = ">=1.35.3,<2" },- { name = "botocore", marker = "extra == 'storage'", specifier = ">=1.35.3,<2" },+ { name = "boto3", marker = "extra == 'all'", specifier = ">=1.35.3,<2" },+ { name = "boto3", marker = "extra == 'model-platforms'", specifier = ">=1.35.3,<2" },+ { name = "boto3", marker = "extra == 'storage'", specifier = ">=1.35.3,<2" },{ name = "chromadb", marker = "extra == 'all'", specifier = ">=0.6.0,<1.0.0" },{ name = "chromadb", marker = "extra == 'rag'", specifier = ">=0.6.0,<1.0.0" },{ name = "chromadb", marker = "extra == 'storage'", specifier = ">=0.6.0,<1.0.0" },@@ -7407,7 +7423,7 @@ name = "pexpect"version = "4.9.0"source = { registry = "https://pypi.org/simple" }dependencies = [- { name = "ptyprocess" },+ { name = "ptyprocess", marker = "sys_platform != 'win32'" },]sdist = { url = "https://files.pythonhosted.org/packages/42/92/cc564bf6381ff43ce1f4d06852fc19a2f11d180f23dc32d9588bee2f149d/pexpect-4.9.0.tar.gz", hash = "sha256:ee7d41123f3c9911050ea2c2dac107568dc43b2d3b0c7557a33212c398ead30f", size = 166450, upload-time = "2023-11-25T09:07:26.339Z" }wheels = [@@ -8402,7 +8418,7 @@ name = "pyobjc-framework-cocoa"version = "12.1"source = { registry = "https://pypi.org/simple" }dependencies = [- { name = "pyobjc-core", marker = "python_full_version >= '3.13' or sys_platform == 'darwin'" },+ { name = "pyobjc-core", marker = "(python_full_version >= '3.13' and sys_platform != 'win32') or sys_platform == 'darwin'" },]sdist = { url = "https://files.pythonhosted.org/packages/02/a3/16ca9a15e77c061a9250afbae2eae26f2e1579eb8ca9462ae2d2c71e1169/pyobjc_framework_cocoa-12.1.tar.gz", hash = "sha256:5556c87db95711b985d5efdaaf01c917ddd41d148b1e52a0c66b1a2e2c5c1640", size = 2772191, upload-time = "2025-11-14T10:13:02.069Z" }wheels = [@@ -8420,8 +8436,8 @@ name = "pyobjc-framework-quartz"version = "12.1"source = { registry = "https://pypi.org/simple" }dependencies = [- { name = "pyobjc-core", marker = "python_full_version >= '3.13' or sys_platform == 'darwin'" },- { name = "pyobjc-framework-cocoa", marker = "python_full_version >= '3.13' or sys_platform == 'darwin'" },+ { name = "pyobjc-core", marker = "(python_full_version >= '3.13' and sys_platform != 'win32') or sys_platform == 'darwin'" },+ { name = "pyobjc-framework-cocoa", marker = "(python_full_version >= '3.13' and sys_platform != 'win32') or sys_platform == 'darwin'" },]sdist = { url = "https://files.pythonhosted.org/packages/94/18/cc59f3d4355c9456fc945eae7fe8797003c4da99212dd531ad1b0de8a0c6/pyobjc_framework_quartz-12.1.tar.gz", hash = "sha256:27f782f3513ac88ec9b6c82d9767eef95a5cf4175ce88a1e5a65875fee799608", size = 3159099, upload-time = "2025-11-14T10:21:24.31Z" }wheels = [@@ -9712,6 +9728,18 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/bd/29/1fe6a1b1811f64bdd35b61300e9d57f68d630d271c92423553a30072d5c5/ruptures-1.1.10-cp313-cp313-win_amd64.whl", hash = "sha256:39f3ece91327440fbebca84155b39435970ccc0f52bd3e33878aaff157d04e01", size = 477215, upload-time = "2025-09-10T09:47:55.027Z" },]+[[package]]+name = "s3transfer"+version = "0.16.0"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "botocore" },+]+sdist = { url = "https://files.pythonhosted.org/packages/05/04/74127fc843314818edfa81b5540e26dd537353b123a4edc563109d8f17dd/s3transfer-0.16.0.tar.gz", hash = "sha256:8e990f13268025792229cd52fa10cb7163744bf56e719e0b9cb925ab79abf920", size = 153827, upload-time = "2025-12-01T02:30:59.114Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/fc/51/727abb13f44c1fcf6d145979e1535a35794db0f6e450a0cb46aa24732fe2/s3transfer-0.16.0-py3-none-any.whl", hash = "sha256:18e25d66fed509e3868dc1572b3f427ff947dd2c56f844a5bf09481ad3f3b2fe", size = 86830, upload-time = "2025-12-01T02:30:57.729Z" },+]+[[package]]name = "safetensors"version = "0.7.0"