|
| 1 | +/** |
| 2 | + * Why a model stopped generating, normalized across providers. |
| 3 | + * |
| 4 | + * Providers disagree on vocabulary for the same event — a truncated generation is |
| 5 | + * `length` on OpenAI, `max_tokens` on Anthropic and Bedrock, and `MAX_TOKENS` on |
| 6 | + * Gemini. Traces keep each provider's raw string because it is the ground truth for |
| 7 | + * debugging; this normalized value exists so a workflow can branch on the outcome |
| 8 | + * without enumerating every provider's spelling. |
| 9 | + */ |
| 10 | +export type AgentFinishReason = |
| 11 | + /** Generation completed naturally, or hit a caller-supplied stop sequence. */ |
| 12 | + | 'stop' |
| 13 | + /** Truncated by a token limit — the model had more to say. */ |
| 14 | + | 'length' |
| 15 | + /** Stopped in order to call tools. */ |
| 16 | + | 'tool_calls' |
| 17 | + /** Blocked or refused by a safety system. */ |
| 18 | + | 'content_filter' |
| 19 | + /** The provider reported the generation itself as malformed. */ |
| 20 | + | 'error' |
| 21 | + /** Reported, but not a case this vocabulary distinguishes. */ |
| 22 | + | 'other' |
| 23 | + |
| 24 | +/** |
| 25 | + * Raw provider value → normalized reason, keyed on the lowercased string. |
| 26 | + * |
| 27 | + * A single table rather than a per-provider mapper because the vocabularies do not |
| 28 | + * collide: no raw value means one thing to one provider and something else to |
| 29 | + * another. Sources are the SDK types this repo compiles against — |
| 30 | + * `ChatCompletion.finish_reason`, Anthropic's `StopReason`, Gemini's `FinishReason`, |
| 31 | + * and Bedrock's `StopReason`. |
| 32 | + */ |
| 33 | +const NORMALIZED_BY_RAW = new Map<string, AgentFinishReason>([ |
| 34 | + // OpenAI Chat Completions, and every OpenAI-compatible provider. |
| 35 | + ['stop', 'stop'], |
| 36 | + ['length', 'length'], |
| 37 | + ['tool_calls', 'tool_calls'], |
| 38 | + ['function_call', 'tool_calls'], |
| 39 | + ['content_filter', 'content_filter'], |
| 40 | + |
| 41 | + // OpenAI Responses reports truncation through `incomplete_details.reason`. |
| 42 | + ['max_output_tokens', 'length'], |
| 43 | + |
| 44 | + // Anthropic Messages, shared by Bedrock's Converse API. |
| 45 | + ['end_turn', 'stop'], |
| 46 | + ['stop_sequence', 'stop'], |
| 47 | + ['max_tokens', 'length'], |
| 48 | + ['model_context_window_exceeded', 'length'], |
| 49 | + ['tool_use', 'tool_calls'], |
| 50 | + ['refusal', 'content_filter'], |
| 51 | + /** A server-tool pause is a continuation point, not an outcome. */ |
| 52 | + ['pause_turn', 'other'], |
| 53 | + |
| 54 | + // Bedrock Converse additions. |
| 55 | + ['content_filtered', 'content_filter'], |
| 56 | + ['guardrail_intervened', 'content_filter'], |
| 57 | + ['malformed_model_output', 'error'], |
| 58 | + ['malformed_tool_use', 'error'], |
| 59 | + |
| 60 | + /** |
| 61 | + * Gemini. `STOP` and `MAX_TOKENS` already lowercase onto the entries above, so |
| 62 | + * only the values with no counterpart elsewhere are listed here. Recitation is a |
| 63 | + * content restriction, so it groups with the filters. |
| 64 | + */ |
| 65 | + ['safety', 'content_filter'], |
| 66 | + ['blocklist', 'content_filter'], |
| 67 | + ['prohibited_content', 'content_filter'], |
| 68 | + ['spii', 'content_filter'], |
| 69 | + ['recitation', 'content_filter'], |
| 70 | + ['image_safety', 'content_filter'], |
| 71 | + ['image_prohibited_content', 'content_filter'], |
| 72 | + ['image_recitation', 'content_filter'], |
| 73 | + ['malformed_function_call', 'error'], |
| 74 | + ['unexpected_tool_call', 'error'], |
| 75 | + ['language', 'other'], |
| 76 | + ['other', 'other'], |
| 77 | + ['no_image', 'other'], |
| 78 | + ['image_other', 'other'], |
| 79 | + ['finish_reason_unspecified', 'other'], |
| 80 | +]) |
| 81 | + |
| 82 | +/** |
| 83 | + * Normalizes a provider's raw stop reason. |
| 84 | + * |
| 85 | + * Returns `undefined` when the provider reported nothing, so an absent reason stays |
| 86 | + * distinguishable from one the vocabulary could not place. An unrecognized value maps |
| 87 | + * to `'other'` rather than throwing: a provider adding a case must not fail a run |
| 88 | + * that otherwise succeeded. |
| 89 | + */ |
| 90 | +export function normalizeFinishReason( |
| 91 | + raw: string | null | undefined |
| 92 | +): AgentFinishReason | undefined { |
| 93 | + if (!raw) return undefined |
| 94 | + return NORMALIZED_BY_RAW.get(raw.trim().toLowerCase()) ?? 'other' |
| 95 | +} |
0 commit comments