pig/ai/stop_reason
A normalized stop reason across all LLM providers.
Each provider maps its native finish/stop reason string to one of these variants. This gives the rest of the codebase a stable, provider-agnostic type to pattern-match on.
Inspired by pi’s StopReason type.
Types
A normalized stop reason across all providers.
pub type StopReason {
Stop
Length
ToolUse
Error
Unknown(String)
}
Constructors
-
StopThe model finished normally (e.g. OpenAI
"stop", Anthropic"end_turn") -
LengthThe model hit a token/length limit (e.g. OpenAI
"length", Anthropic"max_tokens") -
ToolUseThe model requested tool execution (e.g. OpenAI
"tool_calls", Anthropic"tool_use") -
ErrorThe model stopped due to an error or content filter (e.g. OpenAI
"content_filter") -
Unknown(String)An unrecognized value from the provider — preserved for forward compatibility
Values
pub fn decoder() -> decode.Decoder(StopReason)
Decode a StopReason from a JSON string value.
pub fn from_openai(raw: String) -> StopReason
Map an OpenAI native finish_reason string to a StopReason.
| OpenAI value | StopReason |
|---|---|
"stop" | Stop |
"tool_calls" | ToolUse |
"length" | Length |
"content_filter" | Error |
| anything else | Unknown |
pub fn from_string(s: String) -> StopReason
Parse a canonical string back into a StopReason.
Inverse of to_string for known variants.
Unrecognized strings become Unknown.
pub fn to_json(reason: StopReason) -> json.Json
Serialize a StopReason as a JSON string value.
pub fn to_string(reason: StopReason) -> String
Convert a StopReason to its canonical string representation.
Used for JSONL serialization, telemetry, and display.
Unknown values pass through the original provider string.