ResponseHandlers
Namespace: Vercel.AI.ProviderUtils
public static class ResponseHandlersResponse handlers for binary, event stream, and error responses.
Maps to createBinaryResponseHandler, createBinaryStreamResponseHandler,
createEventSourceResponseHandler, createJsonErrorResponseHandler,
and createStatusCodeErrorResponseHandler.
Inheritance: object
Methods
Section titled “Methods”CreateBinaryResponseAsync(HttpResponseMessage, string, object?, CancellationToken)
Section titled “CreateBinaryResponseAsync(HttpResponseMessage, string, object?, CancellationToken)”public static Task<ResponseHandlerResult<byte[]>> CreateBinaryResponseAsync(HttpResponseMessage response, string url, object? requestBodyValues = null, CancellationToken cancellationToken = default)Reads the body as bytes. Maps to createBinaryResponseHandler.
Parameters
| Name | Type | Description |
|---|---|---|
response |
HttpResponseMessage | |
url |
string | |
requestBodyValues |
object | |
cancellationToken |
CancellationToken |
Returns: Task<ResponseHandlerResult<byte[]>>
Exceptions
| Type | Condition |
|---|---|
| ApiException | The response has no body. |
| DownloadError | The body is larger than DefaultMaxBytes. |
CreateBinaryStreamResponseAsync(HttpResponseMessage)
Section titled “CreateBinaryStreamResponseAsync(HttpResponseMessage)”public static Task<ResponseHandlerResult<Stream>> CreateBinaryStreamResponseAsync(HttpResponseMessage response)Passes the body through as a stream. Maps to createBinaryStreamResponseHandler.
Parameters
| Name | Type | Description |
|---|---|---|
response |
HttpResponseMessage |
Returns: Task<ResponseHandlerResult<Stream>>
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | The response has no body. |
CreateEventSourceResponseAsync(HttpResponseMessage, string, object?, JsonNode?, CancellationToken)
Section titled “CreateEventSourceResponseAsync(HttpResponseMessage, string, object?, JsonNode?, CancellationToken)”public static Task<ResponseHandlerResult<IAsyncEnumerable<JsonParseResult>>> CreateEventSourceResponseAsync(HttpResponseMessage response, string url, object? requestBodyValues, JsonNode? schema, CancellationToken cancellationToken = default)Parses a Server-Sent Event body into JSON values. A read failure on the successful response is
rethrown as a retryable ApiException that keeps the status, URL, headers, and cause.
Maps to createEventSourceResponseHandler.
Parameters
| Name | Type | Description |
|---|---|---|
response |
HttpResponseMessage | |
url |
string | |
requestBodyValues |
object | |
schema |
JsonNode | |
cancellationToken |
CancellationToken |
Returns: Task<ResponseHandlerResult<IAsyncEnumerable<JsonParseResult>>>
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | The response has no body. |
CreateJsonErrorResponseAsync(HttpResponseMessage, string, object?, JsonNode?, Func<JsonElement, string>, CancellationToken)
Section titled “CreateJsonErrorResponseAsync(HttpResponseMessage, string, object?, JsonNode?, Func<JsonElement, string>, CancellationToken)”public static Task<ApiException> CreateJsonErrorResponseAsync(HttpResponseMessage response, string url, object? requestBodyValues, JsonNode? errorSchema, Func<JsonElement, string> errorToMessage, CancellationToken cancellationToken = default)Builds an ApiException from an error response whose body is JSON.
The message comes from <code class=“paramref”>errorToMessage</code>. When the body is not valid for the schema, the status text is used.
Maps to createJsonErrorResponseHandler.
Parameters
| Name | Type | Description |
|---|---|---|
response |
HttpResponseMessage | |
url |
string | |
requestBodyValues |
object | |
errorSchema |
JsonNode | |
errorToMessage |
Func<JsonElement, string> | |
cancellationToken |
CancellationToken |
Returns: Task<ApiException>
Exceptions
| Type | Condition |
|---|---|
| DownloadError | The body is larger than DefaultMaxBytes. |
CreateStatusCodeErrorResponseAsync(HttpResponseMessage, string, object?, CancellationToken)
Section titled “CreateStatusCodeErrorResponseAsync(HttpResponseMessage, string, object?, CancellationToken)”public static Task<ApiException> CreateStatusCodeErrorResponseAsync(HttpResponseMessage response, string url, object? requestBodyValues = null, CancellationToken cancellationToken = default)Builds an ApiException whose message is the status text.
Maps to createStatusCodeErrorResponseHandler.
Parameters
| Name | Type | Description |
|---|---|---|
response |
HttpResponseMessage | |
url |
string | |
requestBodyValues |
object | |
cancellationToken |
CancellationToken |
Returns: Task<ApiException>
Exceptions
| Type | Condition |
|---|---|
| DownloadError | The body is larger than DefaultMaxBytes. |
Apache-2.0 · Copyright 2023 Vercel, Inc. · unofficial community port, not an official Vercel product