Skip to content

anthropic_utils

FORCED_TOOL_CHOICE_UNSUPPORTED_MIN_VERSIONS = {'opus': (5, 5), 'sonnet': (5, 5), 'fable': (5, 1), 'mythos': (5, 1)} module-attribute

SAMPLING_PARAMS_DEPRECATED_MIN_VERSIONS = {'opus': (4, 7), 'sonnet': (5, 0), 'fable': (5, 0)} module-attribute

_CLAUDE_VERSION_PATTERN = re.compile('claude-([a-z]+)-(\\d+)(?:-(\\d{1,2})(?!\\d))?') module-attribute

_is_at_or_above(model, min_versions)

Source code in griptape/utils/anthropic_utils.py
def _is_at_or_above(model: str, min_versions: dict[str, tuple[int, int]]) -> bool:
    match = _CLAUDE_VERSION_PATTERN.search(model)
    if match is None:
        return False

    family, major = match.group(1), int(match.group(2))
    minor = int(match.group(3)) if match.group(3) is not None else 0
    min_version = min_versions.get(family)

    return min_version is not None and (major, minor) >= min_version

resolve_structured_output_strategy(model, strategy)

Source code in griptape/utils/anthropic_utils.py
def resolve_structured_output_strategy(model: str, strategy: StructuredOutputStrategy) -> StructuredOutputStrategy:
    if strategy == "tool" and not supports_forced_tool_choice(model):
        return "rule"
    return strategy

supports_forced_tool_choice(model)

Source code in griptape/utils/anthropic_utils.py
def supports_forced_tool_choice(model: str) -> bool:
    return not _is_at_or_above(model, FORCED_TOOL_CHOICE_UNSUPPORTED_MIN_VERSIONS)

supports_sampling_params(model)

Whether a Claude model accepts the temperature, top_p, and top_k sampling parameters.

Support is derived from the Claude model family and version embedded in model rather than a hardcoded model id, so every model in an affected family at or above the deprecating version (for example every Opus release from 4.7 onward, including major-only ids like claude-opus-5) is covered without a per-model change. A model whose minor version is absent is treated as .0.

Source code in griptape/utils/anthropic_utils.py
def supports_sampling_params(model: str) -> bool:
    """Whether a Claude model accepts the temperature, top_p, and top_k sampling parameters.

    Support is derived from the Claude model family and version embedded in ``model`` rather than a
    hardcoded model id, so every model in an affected family at or above the deprecating version
    (for example every Opus release from 4.7 onward, including major-only ids like ``claude-opus-5``)
    is covered without a per-model change. A model whose minor version is absent is treated as ``.0``.
    """
    return not _is_at_or_above(model, SAMPLING_PARAMS_DEPRECATED_MIN_VERSIONS)