fix(ai): handle unknown finish_reason in openai-completions gracefully
Map unknown finish_reason values (e.g. "end" from Ollama/LM Studio) to "stop" instead of throwing, since assistant content is already produced. fixes #2142
This commit is contained in:
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Handle `finish_reason: "end"` from Ollama/LM Studio by mapping it to `"stop"` instead of throwing ([#2142](https://github.com/badlogic/pi-mono/issues/2142))
|
||||||
|
|
||||||
## [0.58.0] - 2026-03-14
|
## [0.58.0] - 2026-03-14
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -730,10 +730,11 @@ function parseChunkUsage(
|
|||||||
return usage;
|
return usage;
|
||||||
}
|
}
|
||||||
|
|
||||||
function mapStopReason(reason: ChatCompletionChunk.Choice["finish_reason"]): StopReason {
|
function mapStopReason(reason: ChatCompletionChunk.Choice["finish_reason"] | string): StopReason {
|
||||||
if (reason === null) return "stop";
|
if (reason === null) return "stop";
|
||||||
switch (reason) {
|
switch (reason) {
|
||||||
case "stop":
|
case "stop":
|
||||||
|
case "end":
|
||||||
return "stop";
|
return "stop";
|
||||||
case "length":
|
case "length":
|
||||||
return "length";
|
return "length";
|
||||||
@@ -743,7 +744,7 @@ function mapStopReason(reason: ChatCompletionChunk.Choice["finish_reason"]): Sto
|
|||||||
case "content_filter":
|
case "content_filter":
|
||||||
return "error";
|
return "error";
|
||||||
default: {
|
default: {
|
||||||
const _exhaustive: never = reason;
|
const _exhaustive: never = reason as never;
|
||||||
throw new Error(`Unhandled stop reason: ${_exhaustive}`);
|
throw new Error(`Unhandled stop reason: ${_exhaustive}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user