Skip to content
Open
29 changes: 29 additions & 0 deletions unstract/sdk1/src/unstract/sdk1/adapters/base1.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@
guardrailConfig: dict | None = None # noqa: N815

@staticmethod
def validate(adapter_metadata: dict[str, "Any"]) -> dict[str, "Any"]:

Check failure on line 1099 in unstract/sdk1/src/unstract/sdk1/adapters/base1.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=Zipstack_unstract&issues=AZ_W4B0H1ZfQYNfnyRdV&open=AZ_W4B0H1ZfQYNfnyRdV&pullRequest=2199
adapter_metadata["model"] = AWSBedrockLLMParameters.validate_model(
adapter_metadata
)
Expand Down Expand Up @@ -1136,6 +1136,24 @@
result_metadata["thinking"] = thinking_config
result_metadata["temperature"] = 1

# Prompt caching is opt-in and applied on the message payload (a
# `cache_control` block on the stable system prompt), not as a LiteLLM
# completion param, so it is excluded from Pydantic validation and
# carried through on the validated dict for the LLM layer to read.
# Only Anthropic/Claude models on Bedrock support prompt caching, so
# don't advertise the flag for other Bedrock families (Titan, Llama,
# etc.). The LLM layer enforces the same model gate; this just keeps the
# validated metadata honest.
# Check both ``model`` and ``model_id`` so callers routing through a
# Bedrock Application Inference Profile (opaque ARN in ``model``, Claude
# id in ``model_id``) still qualify.
bedrock_model_ids = " ".join(
str(result_metadata.get(field, "")) for field in _MODEL_ID_FIELDS
).lower()
enable_prompt_caching = bool(
adapter_metadata.get("enable_prompt_caching", False)
) and ("anthropic" in bedrock_model_ids or "claude" in bedrock_model_ids)

_pack_bedrock_guardrail_config(result_metadata)

# Create validation metadata excluding control fields. `auth_type` is
Expand All @@ -1152,6 +1170,7 @@
"guardrail_identifier",
"guardrail_version",
"guardrail_trace",
"enable_prompt_caching",
)
}

Expand All @@ -1170,6 +1189,7 @@
# lenient. Reads auth_type from result_metadata since validation_
# metadata strips it before Pydantic.
validated = _resolve_bedrock_aws_credentials(result_metadata, validated)
validated["enable_prompt_caching"] = enable_prompt_caching
return _strip_deprecated_sampling_params(validated)

@staticmethod
Expand Down Expand Up @@ -1237,13 +1257,20 @@
result_metadata["thinking"] = thinking_config
result_metadata["temperature"] = 1

# Prompt caching is opt-in and applied on the message payload (a
# `cache_control` block on the stable system prompt), not as a LiteLLM
# completion param, so it is excluded from Pydantic validation and
# carried through on the validated dict for the LLM layer to read.
enable_prompt_caching = bool(adapter_metadata.get("enable_prompt_caching", False))

# Create validation metadata excluding control fields
exclude_fields = (
"enable_thinking",
"budget_tokens",
"thinking",
"enable_extended_context",
"extra_headers",
"enable_prompt_caching",
)
validation_metadata = {
k: v for k, v in result_metadata.items() if k not in exclude_fields
Expand All @@ -1259,6 +1286,8 @@
if enable_extended_context:
validated["extra_headers"] = {"anthropic-beta": "context-1m-2025-08-07"}

validated["enable_prompt_caching"] = enable_prompt_caching

return _strip_deprecated_sampling_params(validated)

@staticmethod
Expand Down
Loading
Loading