From 6bb474f6bf4de7f5d6b868dffec729aef6c32dc9 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Thu, 19 Mar 2026 11:57:03 -0300 Subject: [PATCH] fix: migrate the openai sdk to our internal fork The OpenAI SDK has a bug on dealing with SSE events. This was affected OpenRouter, but we made a workaround for it. See: * https://github.com/charmbracelet/fantasy/pull/166 * https://github.com/charmbracelet/fantasy/pull/169 We made a fix for the SDK, but it wasn't merged yet: * https://github.com/openai/openai-go/pull/621 In the meantime, an user reported that Avian has the same issue, but it's impossible for us to workaround it. Because of this, we had to fork the SDK. We're pinning the `fantasy` branch from our fork: * https://github.com/charmbracelet/openai-go/tree/fantasy --- go.mod | 2 +- go.sum | 4 ++-- providers/azure/azure.go | 4 ++-- providers/openai/call_useragent.go | 2 +- providers/openai/error.go | 2 +- providers/openai/language_model.go | 6 +++--- providers/openai/language_model_hooks.go | 6 +++--- providers/openai/openai.go | 4 ++-- providers/openai/openai_test.go | 2 +- providers/openai/provider_options.go | 2 +- providers/openai/responses_language_model.go | 8 ++++---- providers/openaicompat/language_model_hooks.go | 6 +++--- providers/openaicompat/openaicompat.go | 2 +- providers/openrouter/language_model_hooks.go | 4 ++-- providers/openrouter/openrouter.go | 2 +- providers/vercel/language_model_hooks.go | 4 ++-- providers/vercel/vercel.go | 2 +- 17 files changed, 31 insertions(+), 31 deletions(-) diff --git a/go.mod b/go.mod index 267a187aa4cfb78ce997ab41ca0008603ad8d931..94fb309bed9376b63faf46a26332c5fc317237ae 100644 --- a/go.mod +++ b/go.mod @@ -10,13 +10,13 @@ require ( github.com/aws/aws-sdk-go-v2/config v1.32.12 github.com/aws/smithy-go v1.24.2 github.com/charmbracelet/anthropic-sdk-go v0.0.0-20260223140439-63879b0b8dab + github.com/charmbracelet/openai-go v0.0.0-20260319145158-d0740cc34266 github.com/charmbracelet/x/exp/slice v0.0.0-20250904123553-b4e2667e5ad5 github.com/charmbracelet/x/json v0.2.0 github.com/go-viper/mapstructure/v2 v2.5.0 github.com/google/uuid v1.6.0 github.com/joho/godotenv v1.5.1 github.com/kaptinlin/jsonschema v0.7.5 - github.com/openai/openai-go/v3 v3.28.0 github.com/stretchr/testify v1.11.1 golang.org/x/oauth2 v0.36.0 google.golang.org/genai v1.50.0 diff --git a/go.sum b/go.sum index 51b240077bdfd2a52f53a6b83a0ee7e2089e1ec1..787a55d4930bb950d238da8393692aa59dfe656b 100644 --- a/go.sum +++ b/go.sum @@ -90,6 +90,8 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/charmbracelet/anthropic-sdk-go v0.0.0-20260223140439-63879b0b8dab h1:J7XQLgl9sefgTnTGrmX3xqvp5o6MCiBzEjGv5igAlc4= github.com/charmbracelet/anthropic-sdk-go v0.0.0-20260223140439-63879b0b8dab/go.mod h1:hqlYqR7uPKOKfnNeicUbZp0Ps0GeYFlKYtwh5HGDCx8= +github.com/charmbracelet/openai-go v0.0.0-20260319145158-d0740cc34266 h1:BW/sZtyd1JyYy0h5adMm3tzpNyL857LWjuTRET6OhpY= +github.com/charmbracelet/openai-go v0.0.0-20260319145158-d0740cc34266/go.mod h1:1DahUaExbUZx/jD+FNT2PKP4L9rLE5+ZBRuI8mZjd/E= github.com/charmbracelet/x/exp/slice v0.0.0-20250904123553-b4e2667e5ad5 h1:DTSZxdV9qQagD4iGcAt9RgaRBZtJl01bfKgdLzUzUPI= github.com/charmbracelet/x/exp/slice v0.0.0-20250904123553-b4e2667e5ad5/go.mod h1:vI5nDVMWi6veaYH+0Fmvpbe/+cv/iJfMntdh+N0+Tms= github.com/charmbracelet/x/json v0.2.0 h1:DqB+ZGx2h+Z+1s98HOuOyli+i97wsFQIxP2ZQANTPrQ= @@ -201,8 +203,6 @@ github.com/onsi/ginkgo/v2 v2.23.4 h1:ktYTpKJAVZnDT4VjxSbiBenUjmlL/5QkBEocaWXiQus github.com/onsi/ginkgo/v2 v2.23.4/go.mod h1:Bt66ApGPBFzHyR+JO10Zbt0Gsp4uWxu5mIOTusL46e8= github.com/onsi/gomega v1.37.0 h1:CdEG8g0S133B4OswTDC/5XPSzE1OeP29QOioj2PID2Y= github.com/onsi/gomega v1.37.0/go.mod h1:8D9+Txp43QWKhM24yyOBEdpkzN8FvJyAwecBgsU4KU0= -github.com/openai/openai-go/v3 v3.28.0 h1:2+FfrCVMdGXSQrBv1tLWtokm+BU7+3hJ/8rAHPQ63KM= -github.com/openai/openai-go/v3 v3.28.0/go.mod h1:cdufnVK14cWcT9qA1rRtrXx4FTRsgbDPW7Ia7SS5cZo= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= diff --git a/providers/azure/azure.go b/providers/azure/azure.go index 7435198dc919695b2f81513fc47b4501db89ae9b..bec505cdb8a73c46ca42fdb73e2acba2e792595b 100644 --- a/providers/azure/azure.go +++ b/providers/azure/azure.go @@ -8,8 +8,8 @@ import ( "charm.land/fantasy" "charm.land/fantasy/providers/openai" - "github.com/openai/openai-go/v3/azure" - "github.com/openai/openai-go/v3/option" + "github.com/charmbracelet/openai-go/azure" + "github.com/charmbracelet/openai-go/option" ) type options struct { diff --git a/providers/openai/call_useragent.go b/providers/openai/call_useragent.go index 34252e1ce8da6db11cb4e72ce973466dde78e833..777a19d3fb7723bc9e29e04045bb1a106044036f 100644 --- a/providers/openai/call_useragent.go +++ b/providers/openai/call_useragent.go @@ -3,7 +3,7 @@ package openai import ( "charm.land/fantasy" "charm.land/fantasy/providers/internal/httpheaders" - "github.com/openai/openai-go/v3/option" + "github.com/charmbracelet/openai-go/option" ) // callUARequestOptions returns per-request options that override the diff --git a/providers/openai/error.go b/providers/openai/error.go index 4d487d2ef057d0682f7640b716188712f6f60652..861b79eea14480ac9f9d94342bcf14797c4f2c80 100644 --- a/providers/openai/error.go +++ b/providers/openai/error.go @@ -10,7 +10,7 @@ import ( "strings" "charm.land/fantasy" - "github.com/openai/openai-go/v3" + "github.com/charmbracelet/openai-go" ) var openaiContextPattern = regexp.MustCompile(`maximum context length is (\d+) tokens.*?(?:resulted in|requested) (\d+) tokens`) diff --git a/providers/openai/language_model.go b/providers/openai/language_model.go index 27eb6186255aba2118165d8213d31491e71ceda8..0679aa3f5bcde82adfe30d83330f8438136ccb9c 100644 --- a/providers/openai/language_model.go +++ b/providers/openai/language_model.go @@ -12,11 +12,11 @@ import ( "charm.land/fantasy" "charm.land/fantasy/object" "charm.land/fantasy/schema" + "github.com/charmbracelet/openai-go" + "github.com/charmbracelet/openai-go/packages/param" + "github.com/charmbracelet/openai-go/shared" xjson "github.com/charmbracelet/x/json" "github.com/google/uuid" - "github.com/openai/openai-go/v3" - "github.com/openai/openai-go/v3/packages/param" - "github.com/openai/openai-go/v3/shared" ) type languageModel struct { diff --git a/providers/openai/language_model_hooks.go b/providers/openai/language_model_hooks.go index b3e94ceef29de859cca682bd31df7a3d6e30b5d5..34ed74c999f5c49100312ce8b11f1e5f104550b7 100644 --- a/providers/openai/language_model_hooks.go +++ b/providers/openai/language_model_hooks.go @@ -6,9 +6,9 @@ import ( "strings" "charm.land/fantasy" - "github.com/openai/openai-go/v3" - "github.com/openai/openai-go/v3/packages/param" - "github.com/openai/openai-go/v3/shared" + "github.com/charmbracelet/openai-go" + "github.com/charmbracelet/openai-go/packages/param" + "github.com/charmbracelet/openai-go/shared" ) // LanguageModelPrepareCallFunc is a function that prepares the call for the language model. diff --git a/providers/openai/openai.go b/providers/openai/openai.go index 88d0968b65405b8097b5cab2c83ab17839ad4311..fb9e7838dde7b5d1cbeac5e93f5568230f5452e0 100644 --- a/providers/openai/openai.go +++ b/providers/openai/openai.go @@ -8,8 +8,8 @@ import ( "charm.land/fantasy" "charm.land/fantasy/providers/internal/httpheaders" - "github.com/openai/openai-go/v3" - "github.com/openai/openai-go/v3/option" + "github.com/charmbracelet/openai-go" + "github.com/charmbracelet/openai-go/option" ) const ( diff --git a/providers/openai/openai_test.go b/providers/openai/openai_test.go index f9c7235a14f424e67a8524c5103b5f4ce50092ff..f517e8e3f7553c589955a09a7f8aec8c4678627a 100644 --- a/providers/openai/openai_test.go +++ b/providers/openai/openai_test.go @@ -11,7 +11,7 @@ import ( "testing" "charm.land/fantasy" - "github.com/openai/openai-go/v3/packages/param" + "github.com/charmbracelet/openai-go/packages/param" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/providers/openai/provider_options.go b/providers/openai/provider_options.go index 504d61d4c3e27755cb55a9463c7b73d26894deeb..50abadbb2b042af2a16c229f6f9f1ae69885b4be 100644 --- a/providers/openai/provider_options.go +++ b/providers/openai/provider_options.go @@ -5,7 +5,7 @@ import ( "encoding/json" "charm.land/fantasy" - "github.com/openai/openai-go/v3" + "github.com/charmbracelet/openai-go" ) // ReasoningEffort represents the reasoning effort level for OpenAI models. diff --git a/providers/openai/responses_language_model.go b/providers/openai/responses_language_model.go index 2ec330bbbec5ed9736b97cad48eed6260a5b1007..dc11e5eb6aa6a1a84fc4123709e3307cafb7b07b 100644 --- a/providers/openai/responses_language_model.go +++ b/providers/openai/responses_language_model.go @@ -12,11 +12,11 @@ import ( "charm.land/fantasy" "charm.land/fantasy/object" "charm.land/fantasy/schema" + "github.com/charmbracelet/openai-go" + "github.com/charmbracelet/openai-go/packages/param" + "github.com/charmbracelet/openai-go/responses" + "github.com/charmbracelet/openai-go/shared" "github.com/google/uuid" - "github.com/openai/openai-go/v3" - "github.com/openai/openai-go/v3/packages/param" - "github.com/openai/openai-go/v3/responses" - "github.com/openai/openai-go/v3/shared" ) const topLogprobsMax = 20 diff --git a/providers/openaicompat/language_model_hooks.go b/providers/openaicompat/language_model_hooks.go index 36c847e028504e05c6da3ffc8e8b58c528b59f61..4da86c0f42a2db2392f99548d2d7f440c2e02761 100644 --- a/providers/openaicompat/language_model_hooks.go +++ b/providers/openaicompat/language_model_hooks.go @@ -8,9 +8,9 @@ import ( "charm.land/fantasy" "charm.land/fantasy/providers/openai" - openaisdk "github.com/openai/openai-go/v3" - "github.com/openai/openai-go/v3/packages/param" - "github.com/openai/openai-go/v3/shared" + openaisdk "github.com/charmbracelet/openai-go" + "github.com/charmbracelet/openai-go/packages/param" + "github.com/charmbracelet/openai-go/shared" ) const reasoningStartedCtx = "reasoning_started" diff --git a/providers/openaicompat/openaicompat.go b/providers/openaicompat/openaicompat.go index e7a2908158e19a2e77526e73fe88a183e4027e24..bb376c59e41db1246a6526076d6b607eda8b17d7 100644 --- a/providers/openaicompat/openaicompat.go +++ b/providers/openaicompat/openaicompat.go @@ -4,7 +4,7 @@ package openaicompat import ( "charm.land/fantasy" "charm.land/fantasy/providers/openai" - "github.com/openai/openai-go/v3/option" + "github.com/charmbracelet/openai-go/option" ) type options struct { diff --git a/providers/openrouter/language_model_hooks.go b/providers/openrouter/language_model_hooks.go index 072224693497028a1dd084287bab1b1428fda063..d38fb8b17c3b68bab0f00ca7a0d9413748d01612 100644 --- a/providers/openrouter/language_model_hooks.go +++ b/providers/openrouter/language_model_hooks.go @@ -11,8 +11,8 @@ import ( "charm.land/fantasy/providers/anthropic" "charm.land/fantasy/providers/google" "charm.land/fantasy/providers/openai" - openaisdk "github.com/openai/openai-go/v3" - "github.com/openai/openai-go/v3/packages/param" + openaisdk "github.com/charmbracelet/openai-go" + "github.com/charmbracelet/openai-go/packages/param" ) const reasoningStartedCtx = "reasoning_started" diff --git a/providers/openrouter/openrouter.go b/providers/openrouter/openrouter.go index d21290a598feae94a92137cf9553d97dc251a17d..36b066c0af7085a6e2491933eaeacdd5887e9a40 100644 --- a/providers/openrouter/openrouter.go +++ b/providers/openrouter/openrouter.go @@ -6,7 +6,7 @@ import ( "charm.land/fantasy" "charm.land/fantasy/providers/openai" - "github.com/openai/openai-go/v3/option" + "github.com/charmbracelet/openai-go/option" ) type options struct { diff --git a/providers/vercel/language_model_hooks.go b/providers/vercel/language_model_hooks.go index b39d01ea1f99902ab4e3e4cf9fea6ac48b06bf55..e3b82921006d88ce5c68de3f6d3a56560a0c9ac3 100644 --- a/providers/vercel/language_model_hooks.go +++ b/providers/vercel/language_model_hooks.go @@ -11,8 +11,8 @@ import ( "charm.land/fantasy/providers/anthropic" "charm.land/fantasy/providers/google" openaipkg "charm.land/fantasy/providers/openai" - openaisdk "github.com/openai/openai-go/v3" - "github.com/openai/openai-go/v3/packages/param" + openaisdk "github.com/charmbracelet/openai-go" + "github.com/charmbracelet/openai-go/packages/param" ) const reasoningStartedCtx = "reasoning_started" diff --git a/providers/vercel/vercel.go b/providers/vercel/vercel.go index 79f31f1dfdbcbf830a61ad5bfc36f873bafad9a0..65e8d31c327f2725e2bbe3ee9817cc567a7c780f 100644 --- a/providers/vercel/vercel.go +++ b/providers/vercel/vercel.go @@ -4,7 +4,7 @@ package vercel import ( "charm.land/fantasy" "charm.land/fantasy/providers/openai" - "github.com/openai/openai-go/v3/option" + "github.com/charmbracelet/openai-go/option" ) type options struct {