Commit 934174a
authored
fix: fix tool-calls finish reason conversion in streaming mode for newer gemini models like gemini-3.5-flash (#2399)
**Description**
***Problem***
When the model returns a tool call, OpenAI expects the finish reason to
be `tool_calls` while Gemini returns `STOP`. We convert it via
```
if len(toolCalls) > 0 {
return openai.ChatCompletionChoicesFinishReasonToolCalls
}
```
This was correct for streaming mode because the finish reason and the
tool calls are in the same chunk in the previous Gemini models. However,
for newer Gemini models like gemini-3.5-flash, they are split into
**two** chunks. The `functionCall` chunk has
**no** `finishReason`, and a **trailing** chunk carries `finishReason:
STOP` with an empty
text part and **no** `functionCall`:
```jsonc
// raw GCP chunk 1 — has the tool call, no finishReason
{"candidates":[{"content":{"parts":[{"functionCall":{"name":"get_current_weather","args":{"...":"..."}},
"thoughtSignature":"..."}]}}],
"usageMetadata":{"trafficType":"ON_DEMAND"}}
// raw GCP chunk 2 — terminal STOP, empty text, no functionCall
{"candidates":[{"content":{"parts":[{"text":""}]},"finishReason":"STOP"}],
"usageMetadata":{"promptTokenCount":107,"candidatesTokenCount":31,"thoughtsTokenCount":148,"...":"..."}}
```
In this case, the conversion would be invalid. Thus, we need to remember
whether the model returned a tool call during the streaming mode.
---------
Signed-off-by: yxia216 <yxia216@bloomberg.net>1 parent 0553786 commit 934174a
2 files changed
Lines changed: 77 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
85 | 91 | | |
86 | 92 | | |
87 | 93 | | |
| |||
444 | 450 | | |
445 | 451 | | |
446 | 452 | | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
447 | 459 | | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
448 | 472 | | |
449 | 473 | | |
450 | 474 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1944 | 1944 | | |
1945 | 1945 | | |
1946 | 1946 | | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
1947 | 2000 | | |
1948 | 2001 | | |
1949 | 2002 | | |
| |||
0 commit comments