|
23 | 23 | * |
24 | 24 | * <p>Providers report termination with raw strings (for example {@code "stop"}, {@code "length"}, |
25 | 25 | * {@code "MAX_TOKENS"}), and some report no reason at all. This helper identifies the one case |
26 | | - * callers must treat as incomplete output: the provider explicitly stopped at an output-token |
27 | | - * limit. A missing or unrecognized reason never indicates truncation, so callers keep accepting |
28 | | - * providers that do not report one. |
| 26 | + * callers must treat as incomplete output: the provider stopped at an output-token limit or filled |
| 27 | + * the context window. A missing or unrecognized reason never indicates truncation, so callers keep |
| 28 | + * accepting providers that do not report one. |
29 | 29 | */ |
30 | 30 | public final class FinishReasons { |
31 | 31 |
|
32 | 32 | private FinishReasons() {} |
33 | 33 |
|
34 | 34 | /** |
35 | | - * Returns whether the reported termination means the output was cut at an output-token limit. |
| 35 | + * Returns whether the reported termination means the output was cut short. |
36 | 36 | * |
37 | 37 | * @param finishReason the last reported finish reason, or {@code null} when none was |
38 | 38 | * @param responseMetadata response metadata accompanying that report, or {@code null} |
39 | 39 | */ |
40 | 40 | public static boolean indicatesOutputLimit( |
41 | 41 | String finishReason, Map<String, Object> responseMetadata) { |
42 | 42 | // OpenAI Chat Completions and DashScope report "length"; Anthropic and Gemini report |
43 | | - // "max_tokens" or "MAX_TOKENS". |
| 43 | + // "max_tokens" or "MAX_TOKENS"; Anthropic reports "model_context_window_exceeded" when the |
| 44 | + // response fills the context window instead of reaching its own output limit. |
44 | 45 | if ("length".equalsIgnoreCase(finishReason) |
45 | | - || "max_tokens".equalsIgnoreCase(finishReason)) { |
| 46 | + || "max_tokens".equalsIgnoreCase(finishReason) |
| 47 | + || "model_context_window_exceeded".equalsIgnoreCase(finishReason)) { |
46 | 48 | return true; |
47 | 49 | } |
48 | 50 | // The OpenAI Responses API reports "incomplete" and names the cause in metadata; only the |
|
0 commit comments