Skip to content

invoke_global_inference_gateway async

invoke_global_inference_gateway(
    gateway_id: GatewayId,
    content_type: RequestContentType,
    body: RequestPayloadBlob,
    *,
    config_overrides: Optional[AsyncSageMakerGatewayRuntimeClientConfig] = None,
    accept: Optional[str] = None,
    customer_identity_token: Optional[CustomerIdentityToken] = None,
) -> InvokeGlobalInferenceGatewayOutput

Buffered (non-streaming) inference. Returns the full OpenAI completion body. Errors carry their origin in the Message prefix. Every typed error's text is in the Message member (JSON key Message), and starts with one of these prefixes: * Global Inference Gateway: -- the gateway rejected or failed the request itself: validation, authorization, throttling, routing (the gateway or model was not found, or no capacity is available), a gateway limit or time limit, or failing to reach the inference gateway (Global Inference Gateway: could not reach the inference gateway: <reason>, or Global Inference Gateway: timed out waiting for the inference gateway). This includes the 400 for an invalid request and the 413 for a request body that is too large. * inference gateway: -- the inference gateway serving the model returned an error status. The message gives that status code and up to 256 bytes of its error message, for example inference gateway: 503 <message>. A BadGatewayException can instead read inference gateway: response ended unexpectedly, with no status, when the inference gateway's connection failed while it was sending the response. * inference gateway (<component>): -- the same, when the inference gateway reports which of its components produced the error, for example inference gateway (model server): 502 <message>. The component is one of: * config -- the inference gateway's configuration and proxy layer. * scheduler -- the inference gateway's request scheduler. * model selector -- the inference gateway's model selector. * model server -- the model server. Every error status the inference gateway returns is a typed error, chosen by that status: * 400 and any other 4xx not listed here -- a 400 validation error. * 401 and 403 -- AccessDeniedException (403). * 404 -- ResourceNotFoundException. * 408 -- GatewayTimeoutException (504). * 429 -- ThrottlingException, with the Retry-After header. * 502, and 500, 503 or any other 5xx not listed here -- BadGatewayException (502). A 500 or 503 from the inference gateway comes after the request was dispatched, so it is not modeled as retryable, and the gateway may already have tried another inference gateway. Standard SDK retry modes may still retry a 5xx status; set max attempts to 1 if a repeated inference is unacceptable. * 504 -- GatewayTimeoutException.

Parameters:

  • gateway_id (GatewayId) –

    The ID of the Global Inference Gateway to invoke. IAM policies refer to the gateway by its ARN, arn:<partition>:sagemaker:<region>:<account>:global-inference-gateway/<id>.

  • accept (Optional[str], default: None ) –

    The MIME type you want for the response. It is passed to the model server.

  • content_type (RequestContentType) –

    The MIME type of the request body. Pass application/json (parameters such as charset=utf-8 are allowed): the body is one OpenAI JSON object. The Global Inference Gateway rejects other content types with a 415 error.

  • customer_identity_token (Optional[CustomerIdentityToken], default: None ) –

    Per-caller customer identity token (a JWT), forwarded unchanged to the inference gateway, which validates it. The gateway does not use it to authorize the request. Include it in the SigV4 signed headers so it cannot be altered in transit. Redacted from logs, metrics and traces.

  • body (RequestPayloadBlob) –

    The inference request body: one OpenAI JSON object, at most 10 MiB. Its content is passed to the model server unchanged, except that the request is always sent with "stream": false: this operation returns one JSON document, so a "stream": true in the body is overridden (use InvokeGlobalInferenceGatewayWithResponseStream to stream). A body that is not one JSON object, or that repeats the top-level model key with different values, is rejected with a 400 validation error. A body larger than 10 MiB is rejected with PayloadTooLargeException (413). The gateway reads only model and stream, and does not store the body.

Raises:

  • AccessDeniedException –

    The caller is not authorized to invoke this gateway. Invoking requires the sagemaker:InvokeGlobalInferenceGateway permission, or sagemaker:InvokeGlobalInferenceGatewayWithResponseStream for the streaming operation, on the gateway ARN. A message starting inference gateway means the inference gateway serving the model refused the request with a 401 or 403, for example for its customer identity token. Its status code is in the message (the response status is 403), and up to 256 bytes of its error message are quoted.

  • BadGatewayException –

    502 -- The gateway could not get a usable response from the inference gateway. The Message prefix says why: * Global Inference Gateway: could not reach the inference gateway: <reason> -- the request could not be sent, or the connection closed before a response arrived (for example, the connection failed, or the inference gateway closed it after reading the request). * Global Inference Gateway: timed out waiting for the inference gateway -- on the buffered operation, the inference gateway stopped sending partway through the response body and the gateway's read timeout expired. (The same text on a GatewayTimeoutException means no response arrived.) * Any other Global Inference Gateway: message -- the gateway ended a response before it was complete, for example because it exceeded the 10 MiB limit of the buffered operation, the request exceeded its maximum duration, or the inference gateway was taken out of service. * inference gateway: <status> <message> -- the inference gateway serving the model returned a 502, 500, 503 or another 5xx error. Its status code is in the message (the response status is 502), and up to 256 bytes of its error message are quoted. * inference gateway: response ended unexpectedly -- the inference gateway's connection failed while it was sending the response. The inference gateway names a component in the prefix when it reports one: inference gateway (<component>):, where the component is config (the inference gateway's configuration and proxy layer), scheduler (its request scheduler), model selector (its model selector) or model server (the model server). Not modeled as retryable: a 502 can mean the inference already ran, and the gateway may already have tried another inference gateway. Standard SDK retry modes may still retry a 5xx status, which would run (and bill) the inference again; set max attempts to 1 if a repeated inference is unacceptable.

  • GatewayTimeoutException –

    504 -- The inference gateway did not respond in time. The Message prefix says whose time limit expired: * Global Inference Gateway: timed out waiting for the inference gateway -- the gateway's limit on the wait for the first part of the response. * inference gateway: <status> <message> -- the inference gateway serving the model returned a 504, or a 408 because it timed out waiting for the request to arrive. Its status code is in the message (the response status is 504), and up to 256 bytes of its error message are quoted. The inference gateway names a component in the prefix when it reports one: inference gateway (<component>):, where the component is config (the inference gateway's configuration and proxy layer), scheduler (its request scheduler), model selector (its model selector) or model server (the model server). Not modeled as retryable: a timeout does not prove the inference did not run, and the gateway may already have tried another inference gateway. Standard SDK retry modes may still retry a 5xx status, which could run (and bill) the inference again; set max attempts to 1 if a repeated inference is unacceptable.

  • InternalServerException –

    An unexpected internal failure occurred.

  • PayloadTooLargeException –

    The request body exceeds the 10 MiB limit. Reduce the size of the payload and send the request again.

  • ResourceNotFoundException –

    No gateway with this ID exists in this account and Region, or no target of the gateway serves the model named in the request body. A model that is served but has no available target right now returns ServiceUnavailableException instead. A message starting inference gateway means the inference gateway serving the model returned a 404; up to 256 bytes of its error message are quoted.

  • ServiceUnavailableException –

    The service is temporarily unable to serve the request, for example because no capacity is available for the requested model. The request was not dispatched for inference. A 503 returned by the inference gateway serving the model after dispatch is reported as BadGatewayException, with the 503 in its Message, not as this error.

  • ThrottlingException –

    The request was throttled before it was dispatched for inference. Retry the request with backoff, after the Retry-After seconds when the response carries one. A message starting inference gateway means the last inference gateway tried for the model returned a 429. The gateway may have tried others first, and they may have failed for other reasons. Up to 256 bytes of its error message are quoted.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Source code in docs_src/capo_sagemaker_gateway_runtime/_services/async_sage_maker_gateway_runtime.py
async def invoke_global_inference_gateway(
    self,
    gateway_id: "capo_sagemaker_gateway_runtime.types.gateway_id.GatewayId",
    content_type: "capo_sagemaker_gateway_runtime.types.request_content_type.RequestContentType",
    body: "capo_sagemaker_gateway_runtime.types.request_payload_blob.RequestPayloadBlob",
    *,
    config_overrides: Optional[AsyncSageMakerGatewayRuntimeClientConfig] = None,
    accept: Optional[str] = None,
    customer_identity_token: Optional[
        "capo_sagemaker_gateway_runtime.types.customer_identity_token.CustomerIdentityToken"
    ] = None,
) -> "capo_sagemaker_gateway_runtime.types.invoke_global_inference_gateway_output.InvokeGlobalInferenceGatewayOutput":
    """Buffered (non-streaming) inference. Returns the full OpenAI completion body. Errors carry their origin in the `Message` prefix. Every typed error's text is in the `Message` member (JSON key `Message`), and starts with one of these prefixes: * `Global Inference Gateway: ` -- the gateway rejected or failed the request itself: validation, authorization, throttling, routing (the gateway or model was not found, or no capacity is available), a gateway limit or time limit, or failing to reach the inference gateway (`Global Inference Gateway: could not reach the inference gateway: <reason>`, or `Global Inference Gateway: timed out waiting for the inference gateway`). This includes the 400 for an invalid request and the 413 for a request body that is too large. * `inference gateway: ` -- the inference gateway serving the model returned an error status. The message gives that status code and up to 256 bytes of its error message, for example `inference gateway: 503 <message>`. A BadGatewayException can instead read `inference gateway: response ended unexpectedly`, with no status, when the inference gateway's connection failed while it was sending the response. * `inference gateway (<component>): ` -- the same, when the inference gateway reports which of its components produced the error, for example `inference gateway (model server): 502 <message>`. The component is one of: * `config` -- the inference gateway's configuration and proxy layer. * `scheduler` -- the inference gateway's request scheduler. * `model selector` -- the inference gateway's model selector. * `model server` -- the model server. Every error status the inference gateway returns is a typed error, chosen by that status: * 400 and any other 4xx not listed here -- a 400 validation error. * 401 and 403 -- AccessDeniedException (403). * 404 -- ResourceNotFoundException. * 408 -- GatewayTimeoutException (504). * 429 -- ThrottlingException, with the `Retry-After` header. * 502, and 500, 503 or any other 5xx not listed here -- BadGatewayException (502). A 500 or 503 from the inference gateway comes after the request was dispatched, so it is not modeled as retryable, and the gateway may already have tried another inference gateway. Standard SDK retry modes may still retry a 5xx status; set max attempts to 1 if a repeated inference is unacceptable. * 504 -- GatewayTimeoutException.

    Args:
        gateway_id: The ID of the Global Inference Gateway to invoke. IAM policies refer to the gateway by its ARN, `arn:<partition>:sagemaker:<region>:<account>:global-inference-gateway/<id>`.
        accept: The MIME type you want for the response. It is passed to the model server.
        content_type: The MIME type of the request body. Pass `application/json` (parameters such as `charset=utf-8` are allowed): the body is one OpenAI JSON object. The Global Inference Gateway rejects other content types with a 415 error.
        customer_identity_token: Per-caller customer identity token (a JWT), forwarded unchanged to the inference gateway, which validates it. The gateway does not use it to authorize the request. Include it in the SigV4 signed headers so it cannot be altered in transit. Redacted from logs, metrics and traces.
        body: The inference request body: one OpenAI JSON object, at most 10 MiB. Its content is passed to the model server unchanged, except that the request is always sent with `"stream": false`: this operation returns one JSON document, so a `"stream": true` in the body is overridden (use InvokeGlobalInferenceGatewayWithResponseStream to stream). A body that is not one JSON object, or that repeats the top-level `model` key with different values, is rejected with a 400 validation error. A body larger than 10 MiB is rejected with PayloadTooLargeException (413). The gateway reads only `model` and `stream`, and does not store the body.

    Raises:
        capo_sagemaker_gateway_runtime.errors.access_denied_exception.AccessDeniedException: The caller is not authorized to invoke this gateway. Invoking requires the `sagemaker:InvokeGlobalInferenceGateway` permission, or `sagemaker:InvokeGlobalInferenceGatewayWithResponseStream` for the streaming operation, on the gateway ARN. A message starting `inference gateway` means the inference gateway serving the model refused the request with a 401 or 403, for example for its customer identity token. Its status code is in the message (the response status is 403), and up to 256 bytes of its error message are quoted.
        capo_sagemaker_gateway_runtime.errors.bad_gateway_exception.BadGatewayException: 502 -- The gateway could not get a usable response from the inference gateway. The `Message` prefix says why: * `Global Inference Gateway: could not reach the inference gateway: <reason>` -- the request could not be sent, or the connection closed before a response arrived (for example, the connection failed, or the inference gateway closed it after reading the request). * `Global Inference Gateway: timed out waiting for the inference gateway` -- on the buffered operation, the inference gateway stopped sending partway through the response body and the gateway's read timeout expired. (The same text on a GatewayTimeoutException means no response arrived.) * Any other `Global Inference Gateway: ` message -- the gateway ended a response before it was complete, for example because it exceeded the 10 MiB limit of the buffered operation, the request exceeded its maximum duration, or the inference gateway was taken out of service. * `inference gateway: <status> <message>` -- the inference gateway serving the model returned a 502, 500, 503 or another 5xx error. Its status code is in the message (the response status is 502), and up to 256 bytes of its error message are quoted. * `inference gateway: response ended unexpectedly` -- the inference gateway's connection failed while it was sending the response. The inference gateway names a component in the prefix when it reports one: `inference gateway (<component>): `, where the component is `config` (the inference gateway's configuration and proxy layer), `scheduler` (its request scheduler), `model selector` (its model selector) or `model server` (the model server). Not modeled as retryable: a 502 can mean the inference already ran, and the gateway may already have tried another inference gateway. Standard SDK retry modes may still retry a 5xx status, which would run (and bill) the inference again; set max attempts to 1 if a repeated inference is unacceptable.
        capo_sagemaker_gateway_runtime.errors.gateway_timeout_exception.GatewayTimeoutException: 504 -- The inference gateway did not respond in time. The `Message` prefix says whose time limit expired: * `Global Inference Gateway: timed out waiting for the inference gateway` -- the gateway's limit on the wait for the first part of the response. * `inference gateway: <status> <message>` -- the inference gateway serving the model returned a 504, or a 408 because it timed out waiting for the request to arrive. Its status code is in the message (the response status is 504), and up to 256 bytes of its error message are quoted. The inference gateway names a component in the prefix when it reports one: `inference gateway (<component>): `, where the component is `config` (the inference gateway's configuration and proxy layer), `scheduler` (its request scheduler), `model selector` (its model selector) or `model server` (the model server). Not modeled as retryable: a timeout does not prove the inference did not run, and the gateway may already have tried another inference gateway. Standard SDK retry modes may still retry a 5xx status, which could run (and bill) the inference again; set max attempts to 1 if a repeated inference is unacceptable.
        capo_sagemaker_gateway_runtime.errors.internal_server_exception.InternalServerException: An unexpected internal failure occurred.
        capo_sagemaker_gateway_runtime.errors.payload_too_large_exception.PayloadTooLargeException: The request body exceeds the 10 MiB limit. Reduce the size of the payload and send the request again.
        capo_sagemaker_gateway_runtime.errors.resource_not_found_exception.ResourceNotFoundException: No gateway with this ID exists in this account and Region, or no target of the gateway serves the model named in the request body. A model that is served but has no available target right now returns ServiceUnavailableException instead. A message starting `inference gateway` means the inference gateway serving the model returned a 404; up to 256 bytes of its error message are quoted.
        capo_sagemaker_gateway_runtime.errors.service_unavailable_exception.ServiceUnavailableException: The service is temporarily unable to serve the request, for example because no capacity is available for the requested model. The request was not dispatched for inference. A 503 returned by the inference gateway serving the model after dispatch is reported as BadGatewayException, with the 503 in its `Message`, not as this error.
        capo_sagemaker_gateway_runtime.errors.throttling_exception.ThrottlingException: The request was throttled before it was dispatched for inference. Retry the request with backoff, after the `Retry-After` seconds when the response carries one. A message starting `inference gateway` means the last inference gateway tried for the model returned a 429. The gateway may have tried others first, and they may have failed for other reasons. Up to 256 bytes of its error message are quoted.
        capo_sagemaker_gateway_runtime.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_sagemaker_gateway_runtime.types.invoke_global_inference_gateway_input.InvokeGlobalInferenceGatewayInput]",
    ) -> AsyncOperationResponse[
        "capo_sagemaker_gateway_runtime.types.invoke_global_inference_gateway_output.InvokeGlobalInferenceGatewayOutput"
    ]:
        import capo_sagemaker_gateway_runtime._operations.global_inference_gateway_runtime.invoke_global_inference_gateway

        (
            output,
            http_response,
        ) = await capo_sagemaker_gateway_runtime._operations.global_inference_gateway_runtime.invoke_global_inference_gateway.async_invoke_global_inference_gateway(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_sagemaker_gateway_runtime.types.invoke_global_inference_gateway_input.InvokeGlobalInferenceGatewayInput",
    ) -> dict[str, Any]:
        import capo_sagemaker_gateway_runtime.types.invoke_global_inference_gateway_input

        return capo_sagemaker_gateway_runtime.types.invoke_global_inference_gateway_input.redact_sensitive(
            input_
        )

    def _redact_output(
        output: "capo_sagemaker_gateway_runtime.types.invoke_global_inference_gateway_output.InvokeGlobalInferenceGatewayOutput",
    ) -> dict[str, Any]:
        import capo_sagemaker_gateway_runtime.types.invoke_global_inference_gateway_output

        return capo_sagemaker_gateway_runtime.types.invoke_global_inference_gateway_output.redact_sensitive(
            output
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "InvokeGlobalInferenceGateway",
            redact_input=_redact_input,
            redact_output=_redact_output,
        ),
    )
    input_: capo_sagemaker_gateway_runtime.types.invoke_global_inference_gateway_input.InvokeGlobalInferenceGatewayInput = {
        "gateway_id": gateway_id,
        "content_type": content_type,
        "body": body,
    }
    if accept is not None:
        input_["accept"] = accept
    if customer_identity_token is not None:
        input_["customer_identity_token"] = customer_identity_token

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output