1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/// Semantic finish reason, replacing scattered `Option<String>` matching.
///
/// Each variant captures a distinct end-of-turn signal from the LLM provider.
/// Conversion helpers ([`FinishReason::from_openai`], [`FinishReason::from_anthropic`],
/// [`FinishReason::from_responses`]) normalise the provider-specific strings.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FinishReason {
/// Model finished naturally (OpenAI `"stop"` / Anthropic `"end_turn"`).
Stop,
/// Model requested tool calls (Anthropic `"tool_use"`).
///
/// OpenAI's `"tool_calls"` is consumed at the client layer as
/// `StreamChunk::ToolCall` and never reaches the react loop.
ToolUse,
/// Output was truncated by the token limit.
///
/// - OpenAI: `"length"`
/// - Anthropic: `"max_tokens"`
/// - Responses API: `"incomplete"`
Truncated {
/// Provider-specific reason string, if available.
reason: Option<String>,
},
/// Any other / unknown finish reason.
Other(String),
}
impl FinishReason {
/// Normalise an OpenAI Chat Completions `finish_reason` value.
pub fn from_openai(s: Option<&str>) -> Self {
match s {
Some("stop") => Self::Stop,
Some("length") => Self::Truncated {
reason: Some("length".into()),
},
// "tool_calls" is consumed client-side, never reaches the react loop.
Some(other) => Self::Other(other.to_string()),
None => Self::Stop, // stream ended normally
}
}
/// Normalise an Anthropic `stop_reason` value.
pub fn from_anthropic(s: Option<&str>) -> Self {
match s {
Some("end_turn") => Self::Stop,
Some("tool_use") => Self::ToolUse,
Some("max_tokens") => Self::Truncated {
reason: Some("max_tokens".into()),
},
Some(other) => Self::Other(other.to_string()),
None => Self::Stop,
}
}
/// Normalise an OpenAI Responses API status / incomplete reason.
pub fn from_responses(reason: Option<&str>) -> Self {
match reason {
None => Self::Stop,
Some("incomplete") => Self::Truncated { reason: None },
Some(other) => Self::Other(other.to_string()),
}
}
/// Unified conversion when the provider is unknown at compile time.
///
/// Handles the union of OpenAI Chat and Anthropic stop-reason strings,
/// as well as the Responses API `"incomplete:reason"` format.
/// OpenAI `"tool_calls"` is consumed client-side and never reaches here,
/// so it is not matched.
pub fn from_raw(s: Option<&str>) -> Self {
match s {
Some("stop") | Some("end_turn") => Self::Stop,
Some("tool_use") => Self::ToolUse,
Some("length") => Self::Truncated {
reason: Some("length".into()),
},
Some("max_tokens") => Self::Truncated {
reason: Some("max_tokens".into()),
},
// Responses API: only "incomplete:max_output_tokens" is truncation;
// "incomplete:content_filter" is a safety refusal, not a token limit.
Some("incomplete:max_output_tokens") => Self::Truncated {
reason: Some("incomplete:max_output_tokens".into()),
},
Some(s) if s.starts_with("incomplete:") => Self::Other(s.to_string()),
Some(other) => Self::Other(other.to_string()),
None => Self::Stop,
}
}
/// Returns `true` if the response was truncated by the token limit.
pub fn is_truncated(&self) -> bool {
matches!(self, Self::Truncated { .. })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn openai_stop() {
assert_eq!(FinishReason::from_openai(Some("stop")), FinishReason::Stop);
}
#[test]
fn openai_length() {
assert_eq!(
FinishReason::from_openai(Some("length")),
FinishReason::Truncated {
reason: Some("length".into())
}
);
}
#[test]
fn openai_none() {
assert_eq!(FinishReason::from_openai(None), FinishReason::Stop);
}
#[test]
fn openai_other() {
assert_eq!(
FinishReason::from_openai(Some("content_filter")),
FinishReason::Other("content_filter".into())
);
}
#[test]
fn anthropic_end_turn() {
assert_eq!(
FinishReason::from_anthropic(Some("end_turn")),
FinishReason::Stop
);
}
#[test]
fn anthropic_tool_use() {
assert_eq!(
FinishReason::from_anthropic(Some("tool_use")),
FinishReason::ToolUse
);
}
#[test]
fn anthropic_max_tokens() {
assert_eq!(
FinishReason::from_anthropic(Some("max_tokens")),
FinishReason::Truncated {
reason: Some("max_tokens".into())
}
);
}
#[test]
fn responses_incomplete() {
assert_eq!(
FinishReason::from_responses(Some("incomplete")),
FinishReason::Truncated { reason: None }
);
}
#[test]
fn responses_none() {
assert_eq!(FinishReason::from_responses(None), FinishReason::Stop);
}
#[test]
fn is_truncated_true() {
assert!(FinishReason::Truncated { reason: None }.is_truncated());
assert!(
FinishReason::Truncated {
reason: Some("length".into())
}
.is_truncated()
);
}
#[test]
fn is_truncated_false() {
assert!(!FinishReason::Stop.is_truncated());
assert!(!FinishReason::ToolUse.is_truncated());
assert!(!FinishReason::Other("x".into()).is_truncated());
}
#[test]
fn from_raw_incomplete_prefix() {
assert_eq!(
FinishReason::from_raw(Some("incomplete:max_output_tokens")),
FinishReason::Truncated {
reason: Some("incomplete:max_output_tokens".into())
}
);
// content_filter is a safety refusal, not truncation
assert_eq!(
FinishReason::from_raw(Some("incomplete:content_filter")),
FinishReason::Other("incomplete:content_filter".into())
);
}
}