Skip to content

Commit e27edd3

Browse files
Handle nullable finish_reason in the streamed chunk completion choice
1 parent 11a69ac commit e27edd3

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

core/src/main/scala/sttp/openai/requests/completions/chat/ChatChunkRequestResponseData.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ object ChatChunkRequestResponseData {
2525

2626
case class Choices(
2727
delta: Delta,
28-
finishReason: Option[String],
28+
finishReason: Option[String] = None,
2929
index: Int
3030
)
3131

core/src/test/scala/sttp/openai/fixtures/ChatChunkFixture.scala

+7-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ object ChatChunkFixture {
119119
| "system_fingerprint": "systemFingerprint",
120120
| "choices": [
121121
| {
122+
| "delta": {
123+
| "content": "..."
124+
| },
125+
| "index": 0
126+
| },
127+
| {
122128
| "delta": {
123129
| "role": "assistant",
124130
| "content": " Hi",
@@ -140,7 +146,7 @@ object ChatChunkFixture {
140146
| ]
141147
| },
142148
| "finish_reason": "stop",
143-
| "index": 0
149+
| "index": 1
144150
| }
145151
| ]
146152
|}""".stripMargin

core/src/test/scala/sttp/openai/requests/completions/chat/ChatChunkDataSpec.scala

+18-11
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,31 @@ class ChatChunkDataSpec extends AnyFlatSpec with Matchers with EitherValues {
1717
// given
1818
val jsonResponse = fixtures.ChatChunkFixture.jsonResponse
1919

20-
val delta: Delta = Delta(
21-
role = Some(Role.Assistant),
22-
content = Some(" Hi"),
23-
toolCalls = toolCalls
24-
)
25-
26-
val choices: Choices = Choices(
27-
delta = delta,
28-
finishReason = Some("stop"),
29-
index = 0
20+
val choices = Seq(
21+
Choices(
22+
delta = Delta(
23+
content = Some("...")
24+
),
25+
finishReason = None,
26+
index = 0
27+
),
28+
Choices(
29+
delta = Delta(
30+
role = Some(Role.Assistant),
31+
content = Some(" Hi"),
32+
toolCalls = toolCalls
33+
),
34+
finishReason = Some("stop"),
35+
index = 1
36+
)
3037
)
3138

3239
val expectedResponse: ChatChunkResponse = ChatChunkResponse(
3340
id = "chatcmpl-76FxnKOjnPkDVYTAQ1wK8iUNFJPvR",
3441
`object` = "chat.completion",
3542
created = 1681725687,
3643
model = "gpt-3.5-turbo-0301",
37-
choices = Seq(choices),
44+
choices = choices,
3845
systemFingerprint = Some("systemFingerprint")
3946
)
4047

0 commit comments

Comments
 (0)