Migrate OpenAI client to the official openai-java SDK#194
Merged
Conversation
Replace the hand-rolled Ktor + kotlinx.serialization OpenAI client with the official com.openai:openai-java SDK (already declared in the version catalog). The old GptApi.complete() called .body<ChatCompletionResponse>() unconditionally, so any non-2xx from OpenAI was decoded against the success shape and surfaced as a misleading kotlinx MissingFieldException, hiding the real API error (bad model, auth, quota, etc.). GptApi now wraps OpenAIClient and exposes a single complete(...) that returns the message content; the OkHttp client is built in GptApiModule from the existing "gpt" config (apiKey + organization). Blocking SDK calls run on Dispatchers.IO. GptService and UserNoteService are updated to the new signature; the unused getModels() and the hand-rolled request/response models are removed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replaces the hand-rolled Ktor + kotlinx.serialization OpenAI client (
GptApi) with the officialcom.openai:openai-javaSDK (4.39.1, already declared in the version catalog).Why
Production logs showed
UserNoteServicenote generation failing with a misleading error:GptApi.complete()called.body<ChatCompletionResponse>()unconditionally, with no status check. So any non-2xx response from OpenAI (its body is{"error": {...}}) was decoded against the success shape and surfaced as a generic "all fields missing" deserialization error — hiding the real API error (bad model, auth, quota, etc.).The official SDK throws typed errors on failure, so the actual OpenAI error now reaches
UserNoteService'scatchand gets logged. It also removes the hand-maintained request/response models.Changes
GptApinow wrapsOpenAIClientand exposes a singlecomplete(model, systemPrompt, userPrompt, temperature, maxTokens, …)returning the message content. Blocking SDK calls run onDispatchers.IO.GptApiModulebuilds theOpenAIOkHttpClientfrom the existing"gpt"config (apiKey+organization); no longer depends onHttpClientModule.GptServiceandUserNoteServiceupdated to the new signature.getModels()and all hand-rolledChatCompletionRequest/ChatCompletionResponse/OpenApiList/Modelmodels.tgptmodule (OpenAiModule/OpenAiService).Notes
gpt-5.4-mini), and all params (temperature/topP/penalties/maxTokens) are preserved.GarbageMessagesmapping NPE seen in the same logs was fixed independently onmain(38f4367) and is not part of this PR.Test
./gradlew :tgkotbot:compileKotlin :tgkotbot:compileTestKotlin— green (KSP regenerates the DI builder for the newGptApiModuleconstructor).🤖 Generated with Claude Code