Skip to main content

FinishReason

Enum FinishReason 

Source
#[non_exhaustive]
pub enum FinishReason { Stop, Length, ToolCalls, ContentFilter, Other(String), MaxToolIterations, Unspecified, }
Expand description

Why the model stopped generating.

OpenAI-compatible servers report this as a finish_reason string on the final streaming chunk. The SDK maps the well-known values onto variants and preserves anything else verbatim in FinishReason::Other, so a provider-specific reason is never silently flattened into a generic one.

§Why Unspecified exists

finish_reason is optional in practice. llama.cpp, vLLM, and several local gateways stream content and then close the connection (or send data: [DONE]) with finish_reason still null. Reporting that as FinishReason::Stop would claim the model finished cleanly when the SDK has no evidence either way, so it is reported as FinishReason::Unspecified instead — a distinct, checkable state.

§Examples

The distinction that matters for callers parsing structured output:

use open_agent::FinishReason;

// A truncated response is worth retrying with a larger budget.
assert!(FinishReason::Length.is_truncated());
// A clean stop that produced unparseable output is a model behaviour problem.
assert!(!FinishReason::Stop.is_truncated());

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Stop

Generation completed naturally ("stop").

§

Length

Generation was cut off at the token limit ("length").

The content delivered before this point is a prefix of what the model intended to say.

§

ToolCalls

The model finished in order to call tools ("tool_calls").

§

ContentFilter

Generation was halted by a content filter ("content_filter").

§

Other(String)

A reason the SDK does not recognise, preserved exactly as the server sent it.

§

MaxToolIterations

The SDK’s automatic tool-execution loop stopped at max_tool_iterations.

Unlike every other variant this does not come from the server: generation was cut short by the client, and the model’s own finish_reason for the last round would have been ToolCalls — an accurate answer to a different question than “why did this operation stop?”. Reported only by Client::finish_reason() in auto-execution mode; it never appears in a StreamEvent::Finish, and FinishReason::from_wire never produces it.

§

Unspecified

The stream ended without the server ever reporting a reason.

This is not an error — it is the normal behaviour of several OpenAI-compatible servers. It means “no information”, which is deliberately distinct from FinishReason::Stop.

Implementations§

Source§

impl FinishReason

Source

pub fn from_wire(raw: &str) -> Self

Maps a raw finish_reason string from the wire onto a variant.

Matching is ASCII-case-insensitive because servers are inconsistent about casing. Unrecognised values are preserved verbatim (original casing intact) in FinishReason::Other.

§Examples
use open_agent::FinishReason;

assert_eq!(FinishReason::from_wire("length"), FinishReason::Length);
assert_eq!(
    FinishReason::from_wire("ERR"),
    FinishReason::Other("ERR".to_string())
);
Source

pub fn as_str(&self) -> &str

Returns the canonical wire string for this reason.

FinishReason::Unspecified has no wire representation — it is reported as "unspecified" so it can be logged without being mistaken for "stop".

Source

pub fn is_truncated(&self) -> bool

Returns true when generation was cut short by the token limit.

This is the signal that a partial or unparseable response is the SDK caller’s budget problem rather than a model that refused to answer in the requested format.

Trait Implementations§

Source§

impl Clone for FinishReason

Source§

fn clone(&self) -> FinishReason

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FinishReason

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for FinishReason

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for FinishReason

Source§

impl PartialEq for FinishReason

Source§

fn eq(&self, other: &FinishReason) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for FinishReason

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more