def completion_with_retry(
    client: Any,
    model: str,
    request_body: str,
    max_retries: int,
    stream: bool = False,
    guardrail_identifier: Optional[str] = None,
    guardrail_version: Optional[str] = None,
    trace: Optional[str] = None,
    **kwargs: Any,
) -> Any:
    """Use tenacity to retry the completion call."""
    retry_decorator = _create_retry_decorator(client=client, max_retries=max_retries)

    @retry_decorator
    def _completion_with_retry(**kwargs: Any) -> Any:
        if stream:
            if guardrail_identifier is None or guardrail_version is None:
                return client.invoke_model_with_response_stream(
                    modelId=model,
                    body=request_body,
                )
            else:
                return client.invoke_model_with_response_stream(
                    modelId=model,
                    body=request_body,
                    guardrailIdentifier=guardrail_identifier,
                    guardrailVersion=guardrail_version,
                    trace=trace,
                )
        else:
            if guardrail_identifier is None or guardrail_version is None:
                return client.invoke_model(modelId=model, body=request_body)
            else:
                return client.invoke_model(
                    modelId=model,
                    body=request_body,
                    guardrailIdentifier=guardrail_identifier,
                    guardrailVersion=guardrail_version,
                    trace=trace,
                )

    return _completion_with_retry(**kwargs)
Use tenacity to retry the completion call.
llama-index-integrations/llms/llama-index-llms-bedrock/llama_index/llms/bedrock/utils.py