invoke_global_inference_gateway_with_response_stream
async
¶
invoke_global_inference_gateway_with_response_stream(
gateway_id: GatewayId,
content_type: RequestContentType,
body: RequestPayloadBlob,
*,
config_overrides: Optional[AsyncSageMakerGatewayRuntimeClientConfig] = None,
accept: Optional[str] = None,
customer_identity_token: Optional[CustomerIdentityToken] = None,
) -> AsyncGenerator[InvokeGlobalInferenceGatewayWithResponseStreamOutput]
Streaming inference. Returns an AWS event stream of PayloadPart events that carry the model server's server-sent event (SSE) bytes, so SDKs decode the stream into events for you. The errors listed for this operation, other than ModelStreamError and InternalStreamFailure, are returned before the response starts (before the 200 status), so they end the request, not the stream. None of them can occur after the 200. ModelStreamError and InternalStreamFailure appear only as events in the stream, after the 200. Once the 200 is sent, the stream carries the response as PayloadPart events. If reading the inference gateway's response fails, the stream carries one InternalStreamFailure event and then ends. ModelStreamError is reserved and is not currently sent. 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>. * 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. An InternalStreamFailure event's Message uses the same prefixes, with the same text the buffered operation uses for the same failure. Its inference gateway: message carries no status: inference gateway: response ended unexpectedly when the inference gateway's connection failed while it was sending the response. Every error status the inference gateway returns before the 200 is a typed error, chosen by that status as for InvokeGlobalInferenceGateway: * 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, as for InvokeGlobalInferenceGateway.
-
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, as for InvokeGlobalInferenceGateway. Pass
application/json. -
customer_identity_token(Optional[CustomerIdentityToken], default:None) –The customer identity token (a JWT), as for InvokeGlobalInferenceGateway. It is passed unchanged to the inference gateway, which validates it.
-
body(RequestPayloadBlob) –The inference request body, as for InvokeGlobalInferenceGateway, except that the request is always sent with
"stream": true: this operation returns an event stream, so a"stream": falsein the body (or nostreamkey) is overridden. A body larger than 10 MiB is rejected with PayloadTooLargeException (413).
Raises:
-
AccessDeniedException–The caller is not authorized to invoke this gateway. Invoking requires the
sagemaker:InvokeGlobalInferenceGatewaypermission, orsagemaker:InvokeGlobalInferenceGatewayWithResponseStreamfor the streaming operation, on the gateway ARN. A message startinginference gatewaymeans 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
Messageprefix 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 otherGlobal 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 isconfig(the inference gateway's configuration and proxy layer),scheduler(its request scheduler),model selector(its model selector) ormodel 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
Messageprefix 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 isconfig(the inference gateway's configuration and proxy layer),scheduler(its request scheduler),model selector(its model selector) ormodel 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.
-
InternalStreamFailure–A failure after the stream started. The
Messagesays where it came from:Global Inference Gateway:when a gateway time limit expired or the inference gateway was taken out of service, orinference gateway: response ended unexpectedlywhen the inference gateway's connection failed while it was sending the response. The stream ends after this event. -
ModelStreamError–Reserved for a model error during the stream. Not currently sent; a failure after the stream starts is reported as InternalStreamFailure.
-
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 gatewaymeans 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-Afterseconds when the response carries one. A message startinginference gatewaymeans 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
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |