JsonStreams
Namespace: Vercel.AI.ProviderUtils
public static class JsonStreamsReads JSON and Server-Sent Event bodies, including response headers.
Inheritance: object
Fields
Section titled “Fields”DefaultMaxLineBytes
Section titled “DefaultMaxLineBytes”public const int DefaultMaxLineBytes = 67108864Default largest JSON Lines row, in UTF-8 bytes.
Value: int
Methods
Section titled “Methods”ExtractResponseHeaders(HttpResponseMessage)
Section titled “ExtractResponseHeaders(HttpResponseMessage)”public static IReadOnlyDictionary<string, string> ExtractResponseHeaders(HttpResponseMessage response)Lower-cases response and content header names and joins repeated values with a comma.
Parameters
| Name | Type | Description |
|---|---|---|
response |
HttpResponseMessage |
Returns: IReadOnlyDictionary<string, string>
ReadJsonAsync(HttpResponseMessage, JsonNode?, CancellationToken)
Section titled “ReadJsonAsync(HttpResponseMessage, JsonNode?, CancellationToken)”public static Task<JsonStreams.JsonBody> ReadJsonAsync(HttpResponseMessage response, JsonNode? schema, CancellationToken cancellationToken = default)Reads a JSON response. Unknown object properties are removed when the schema says so. A body above DefaultMaxBytes throws a DownloadError before it is read.
Parameters
| Name | Type | Description |
|---|---|---|
response |
HttpResponseMessage | |
schema |
JsonNode | |
cancellationToken |
CancellationToken |
Returns: Task<JsonStreams.JsonBody>
ReadJsonEventsAsync(Stream, JsonNode?, CancellationToken)
Section titled “ReadJsonEventsAsync(Stream, JsonNode?, CancellationToken)”public static IAsyncEnumerable<JsonParseResult> ReadJsonEventsAsync(Stream stream, JsonNode? schema = null, CancellationToken cancellationToken = default)Parses a Server-Sent Event stream into JSON values. data: [DONE] is ignored.
Events are validated with <code class=“paramref”>schema</code> when it is supplied.
Parameters
| Name | Type | Description |
|---|---|---|
stream |
Stream | |
schema |
JsonNode | |
cancellationToken |
CancellationToken |
Returns: IAsyncEnumerable<JsonParseResult>
ReadJsonLinesAsync(Stream?, JsonNode?, Action?, CancellationToken, int, string)
Section titled “ReadJsonLinesAsync(Stream?, JsonNode?, Action?, CancellationToken, int, string)”public static IAsyncEnumerable<JsonElement> ReadJsonLinesAsync(Stream? stream, JsonNode? schema = null, Action? onCancel = null, CancellationToken cancellationToken = default, int maxLineBytes = 67108864, string url = "")Yields one JSON value per non-empty line. Lines may be split across reads. A final line without a newline is still yielded. Early disposal cancels <code class=“paramref”>onCancel</code>. A row longer than <code class=“paramref”>maxLineBytes</code> UTF-8 bytes (the newline excluded) throws a DownloadError for <code class=“paramref”>url</code> and cancels the body.
Parameters
| Name | Type | Description |
|---|---|---|
stream |
Stream | |
schema |
JsonNode | |
onCancel |
Action | |
cancellationToken |
CancellationToken | |
maxLineBytes |
int | |
url |
string |
Returns: IAsyncEnumerable<JsonElement>
Apache-2.0 · Copyright 2023 Vercel, Inc. · unofficial community port, not an official Vercel product