Use json v2

This commit is contained in:
dwrz
2026-10-11 00:55:57 +00:00
parent 81b981c07f
commit 75040646e3
5 changed files with 23 additions and 24 deletions
+4 -4
View File
@@ -4,7 +4,7 @@ import (
"bufio"
"bytes"
"context"
"encoding/json"
"encoding/json/v2"
"fmt"
"io"
"log/slog"
@@ -83,7 +83,7 @@ func (c *Client) completions(
ThinkingBudgetTokens: p.ThinkingBudgetTokens,
ChatTemplateKWARGS: kwargs,
ParallelToolCalls: new(true),
})
}, json.FormatNilSliceAsNull(true))
if err != nil {
return nil, fmt.Errorf(
"completions: marshal request body: %w", err,
@@ -152,7 +152,7 @@ func (c *Client) completions(
}
var cr = &ChatResponse{}
if err := json.NewDecoder(res.Body).Decode(cr); err != nil {
if err := json.UnmarshalRead(res.Body, cr); err != nil {
return nil, fmt.Errorf("completions: decode response: %w", err)
}
if len(cr.Choices) == 0 {
@@ -283,7 +283,7 @@ func (c *Client) CompletionsStream(
StreamOptions: &StreamOptions{IncludeUsage: true},
ThinkingBudgetTokens: p.ThinkingBudgetTokens,
Tools: p.Tools,
})
}, json.FormatNilSliceAsNull(true))
if err != nil {
cancel()
return nil, fmt.Errorf(
+15 -16
View File
@@ -2,8 +2,7 @@ package llm
import (
"context"
"encoding/json"
jsonv2 "encoding/json/v2"
"encoding/json/v2"
"errors"
"fmt"
"io"
@@ -457,7 +456,7 @@ func TestCompletionsReturnsUsageAndTimings(t *testing.T) {
t.Errorf("unexpected path: %s", r.URL.Path)
}
var request ChatRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
if err := json.UnmarshalRead(r.Body, &request); err != nil {
t.Error(err)
}
if request.LogProbs || request.TopLogProbs != 0 {
@@ -480,17 +479,17 @@ func TestCompletionsReturnsUsageAndTimings(t *testing.T) {
if !reflect.DeepEqual(res.Metadata, want) {
t.Errorf("metadata = %#v, want %#v", res.Metadata, want)
}
encoded, err := jsonv2.Marshal(
res.Metadata, jsonv2.FormatNilSliceAsNull(true),
encoded, err := json.Marshal(
res.Metadata, json.FormatNilSliceAsNull(true),
)
if err != nil {
t.Fatal(err)
}
var gotJSON, wantJSON map[string]any
if err := jsonv2.Unmarshal(encoded, &gotJSON); err != nil {
if err := json.Unmarshal(encoded, &gotJSON); err != nil {
t.Fatal(err)
}
if err := jsonv2.Unmarshal([]byte(tt.want), &wantJSON); err != nil {
if err := json.Unmarshal([]byte(tt.want), &wantJSON); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(gotJSON, wantJSON) {
@@ -713,9 +712,9 @@ data: {"choices":[{"index":0,"delta":{},"logprobs":{"content":null,"refusal":[{"
wantChunks: 1,
},
{
name: "omitted choice index and metadata",
response: "data: {\"choices\":[{\"delta\":{\"content\":\"answer\"}}]}\n\n",
wantText: "answer",
name: "omitted choice index and metadata",
response: "data: {\"choices\":[{\"delta\":{\"content\":\"answer\"}}]}\n\n",
wantText: "answer",
wantMetadata: `{
"choice_index":null,
"created":null,
@@ -730,14 +729,14 @@ data: {"choices":[{"index":0,"delta":{},"logprobs":{"content":null,"refusal":[{"
"timings":null,
"usage":null
}`,
wantChunks: 1,
wantChunks: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := newCompletionTestClient(t, func(w http.ResponseWriter, r *http.Request) {
var request ChatRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
if err := json.UnmarshalRead(r.Body, &request); err != nil {
t.Error(err)
}
if request.LogProbs || request.TopLogProbs != 0 {
@@ -761,17 +760,17 @@ data: {"choices":[{"index":0,"delta":{},"logprobs":{"content":null,"refusal":[{"
if err := json.Unmarshal([]byte(tt.wantMetadata), &want); err != nil {
t.Fatal(err)
}
encoded, err := jsonv2.Marshal(
result.Metadata, jsonv2.FormatNilSliceAsNull(true),
encoded, err := json.Marshal(
result.Metadata, json.FormatNilSliceAsNull(true),
)
if err != nil {
t.Fatal(err)
}
var gotJSON, wantJSON map[string]any
if err := jsonv2.Unmarshal(encoded, &gotJSON); err != nil {
if err := json.Unmarshal(encoded, &gotJSON); err != nil {
t.Fatal(err)
}
if err := jsonv2.Unmarshal([]byte(tt.wantMetadata), &wantJSON); err != nil {
if err := json.Unmarshal([]byte(tt.wantMetadata), &wantJSON); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(gotJSON, wantJSON) {
+2 -2
View File
@@ -2,7 +2,7 @@ package llm
import (
"context"
"encoding/json"
"encoding/json/v2"
"fmt"
"io"
"log/slog"
@@ -74,7 +74,7 @@ func (c *Client) ListModels(ctx context.Context) ([]Model, error) {
}
var body ModelsResponse
if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(res.Body, &body); err != nil {
return nil, fmt.Errorf(
"list models: decode response: %w", err,
)
+1 -1
View File
@@ -3,7 +3,7 @@
package llm
import (
"encoding/json"
"encoding/json/v2"
"strings"
)
+1 -1
View File
@@ -2,7 +2,7 @@ package llm
import (
"context"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"io"