SageMakerRuntimeHTTP2Client.invoke_endpoint_with_bidirectional_stream
rboto-sagemaker-runtime-http2==0.4.0 · Service index · Types
Model summary (excerpt): Invokes a model endpoint with bidirectional streaming capabilities. This operation establishes a persistent connection that allows you to send multiple requests and receive streaming responses from the model in real-time. Bidirectional streaming is useful for interactive applications such as chatbots, real-time translation, or any scenario where you need to maintain a conversation-like...
Install and imports
from rboto import SageMakerRuntimeHTTP2Client
from collections.abc import AsyncIterator
from rboto_sagemaker_runtime_http2.types import (
InvokeEndpointWithBidirectionalStreamOutput,
RequestStreamEvent,
)
Signature
async def invoke_endpoint_with_bidirectional_stream(
*,
endpoint_name: str,
body: AsyncIterator[RequestStreamEvent],
target_variant: str | None = None,
model_invocation_path: str | None = None,
model_query_string: str | None = None,
) -> InvokeEndpointWithBidirectionalStreamOutput: ...
Arguments without defaults are required. None omits an optional request argument. Static types do not validate resource existence, permissions, lengths, ranges, or conditional service requirements.
Arguments
| Argument | Required | Model notes (excerpt) |
|---|---|---|
endpoint_name |
Yes | The name of the endpoint to invoke. |
body |
Yes | The request payload stream. |
target_variant |
No | Target variant for the request. |
model_invocation_path |
No | Model invocation path. |
model_query_string |
No | Model query string. |
Output
InvokeEndpointWithBidirectionalStreamOutput.
Typed response mapping, accessed as response["field"].
| Field | Type | Presence | References |
|---|---|---|---|
body |
AsyncIterator[ResponseStreamEvent] |
Always present | ResponseStreamEvent |
invoked_production_variant |
str | None |
Always present | — |
| None means a value can be absent. Output mappings include every declared key; optional input keys may be omitted. Unknown output enum/union variants are not automatically valid inputs.
Related types
InvokeEndpointWithBidirectionalStreamOutput, RequestStreamEvent
Follow these links for nested inputs and outputs; recursive types link back to the same type page.
Pagination and streaming
No generated paginator for this operation.
Output streams: InvokeEndpointWithBidirectionalStreamOutput.
Consume with async for. Do not reuse a consumed stream.
Input event streams accept the declared AsyncIterator of typed events.
Errors
Import from rboto_sagemaker_runtime_http2.exceptions:
InputValidationError, InternalServerError, InternalStreamFailureError, ModelError, ModelStreamError, ServiceUnavailableError.
Stream decoding may also raise the linked modeled event errors.
Other service failures use the service base error; credential, transport and timeout failures use shared errors.
Example
Complete, type-checked service example. Replace its placeholders and configure credentials before running it.
import asyncio
from collections.abc import AsyncIterator
from pathlib import Path
from rboto import SageMakerRuntimeHTTP2Client
from rboto_sagemaker_runtime_http2.types import RequestStreamEvent
async def request_events() -> AsyncIterator[RequestStreamEvent]:
# Use an endpoint whose model accepts this JSON request format.
# Models may require several payload parts or a different content format.
payload = await asyncio.to_thread(Path("request.json").read_bytes)
yield {
"payload_part": {
"bytes": payload,
"data_type": "UTF8",
"completion_state": "COMPLETE",
},
}
async def main() -> None:
client = SageMakerRuntimeHTTP2Client(region="us-east-1")
response = await client.invoke_endpoint_with_bidirectional_stream(
endpoint_name="my-streaming-endpoint",
body=request_events(),
)
async for event in response["body"]:
print(event)
if __name__ == "__main__":
asyncio.run(main())