CJCrafter/ChatGPT-Java-API

EOFException and MalformedJsonException

SeyedAlirezaFatemi opened this issue · 3 comments

I have this simple code:

val request = ChatRequest(model = "gpt-3.5-turbo", messages = mutableListOf("Hi. How are you?".toUserMessage()))
val openai = OpenAI(getOpenAIToken(), null, OkHttpClient.Builder().readTimeout(120, TimeUnit.SECONDS).build())
// This line will block
val response = openai.createChatCompletion(request)

but I get the following exceptions:

com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 2 path $.
	at com.google.gson.internal.Streams.parse(Streams.java:59)
	at com.google.gson.JsonParser.parseReader(JsonParser.java:102)
	at com.google.gson.JsonParser.parseReader(JsonParser.java:70)
	at com.google.gson.JsonParser.parseString(JsonParser.java:51)
	at com.cjcrafter.openai.OpenAICallback.handleStream(OpenAICallback.kt:66)
	at com.cjcrafter.openai.OpenAICallback.onResponse(OpenAICallback.kt:30)
	at com.cjcrafter.openai.OpenAI.createChatCompletion(OpenAI.kt:252)
...
Caused by: java.io.EOFException: End of input at line 1 column 2 path $.
	at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1457)
	at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:503)
	at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:422)
	at com.google.gson.internal.bind.TypeAdapters$28.read(TypeAdapters.java:779)
	at com.google.gson.internal.bind.TypeAdapters$28.read(TypeAdapters.java:725)
	at com.google.gson.internal.Streams.parse(Streams.java:49)

and then lots of this exceptions:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 8 path $
	at com.google.gson.JsonParser.parseReader(JsonParser.java:76)
	at com.google.gson.JsonParser.parseString(JsonParser.java:51)
	at com.cjcrafter.openai.OpenAICallback.handleStream(OpenAICallback.kt:66)
	at com.cjcrafter.openai.OpenAICallback.onResponse(OpenAICallback.kt:30)
	at com.cjcrafter.openai.OpenAI.createChatCompletion(OpenAI.kt:252)

and finally this:

lateinit property response has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property response has not been initialized
	at com.cjcrafter.openai.OpenAI.createChatCompletion(OpenAI.kt:254)

I have the same problem

I have been able to replicate that, it happens when you stream a response but the request is invalid. Usually this means you put in too many tokens.

This exception occurs because OpenAI, for whatever reason, decides to print pretty json in their https response.

Solved in 1.3.2