Skip to content

ResponseHandlers

Namespace: Vercel.AI.ProviderUtils

public static class ResponseHandlers

Response handlers for binary, event stream, and error responses. Maps to createBinaryResponseHandler, createBinaryStreamResponseHandler, createEventSourceResponseHandler, createJsonErrorResponseHandler, and createStatusCodeErrorResponseHandler.

Inheritance: object

View source

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.

View source

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.

View source

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.

View source

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.

View source

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.

View source

Apache-2.0 · Copyright 2023 Vercel, Inc. · unofficial community port, not an official Vercel product