Skip to main content

ModelResponse

Struct ModelResponse 

Source
pub struct ModelResponse {
    pub request_id: RequestId,
    pub provider: ProviderKey,
    pub model: ModelKey,
    pub content: Vec<ContentPart>,
    pub finish: FinishReason,
    pub usage: TokenUsage,
    pub raw_id: Option<String>,
    pub latency: Duration,
    pub warnings: Vec<ResponseWarning>,
}
Expand description

One model answer (spec §20.1).

Fields§

§request_id: RequestId

The request this answers. Equal to ModelRequest::request_id even after a retry.

§provider: ProviderKey

Which provider answered.

§model: ModelKey

Which model answered. May differ from the requested key when the provider resolved an alias; the reported model is recorded.

§content: Vec<ContentPart>

The answer, in order.

§finish: FinishReason

Why generation stopped.

§usage: TokenUsage

Reported tokens.

§raw_id: Option<String>

The provider’s own response identifier, for support tickets. Never a body and never a header.

§latency: Duration

Wall-clock duration of the call, measured by the adapter.

§warnings: Vec<ResponseWarning>

Adapter warnings.

Implementations§

Source§

impl ModelResponse

Source

pub fn new( request_id: RequestId, provider: impl Into<ProviderKey>, model: impl Into<ModelKey>, ) -> Self

A response with no content, for adapters to build on.

Source

pub fn with_part(self, part: ContentPart) -> Self

Appends a content part.

Source

pub fn with_text(self, text: impl Into<String>) -> Self

Appends a text part.

Source

pub fn with_tool_call(self, call: ToolCall) -> Self

Appends a tool call.

Source

pub const fn with_finish(self, finish: FinishReason) -> Self

Sets the finish reason.

Source

pub const fn with_usage(self, usage: TokenUsage) -> Self

Sets the reported usage.

Source

pub fn with_raw_id(self, raw_id: impl Into<String>) -> Self

Sets the provider’s response identifier.

Source

pub const fn with_latency(self, latency: Duration) -> Self

Sets the measured latency.

Source

pub fn with_warning(self, warning: ResponseWarning) -> Self

Adds a warning.

Source

pub fn reference(&self) -> ModelRef

The provider-model pair that answered.

Source

pub fn text(&self) -> String

Every text part, concatenated in order.

use turnframe_provider::prelude::*;

let response = ModelResponse::new(RequestId::nil(), "openai", "gpt-4o")
    .with_text("Ho preparato ")
    .with_text("la modifica.");
assert_eq!(response.text(), "Ho preparato la modifica.");
Source

pub fn tool_calls(&self) -> Vec<&ToolCall>

Every tool call, in the order the model produced them.

Source

pub fn tool_call(&self, id: &CallId) -> Option<&ToolCall>

The call with this id, when present.

Source

pub fn is_empty(&self) -> bool

Returns true when the model produced neither text nor a tool call.

Source

pub fn single_json(&self) -> Result<Value, StructuredOutputError>

Extracts the single JSON document a structured stage expects.

The rule is deterministic and refuses to guess:

  1. A FinishReason::Refusal is a Refusal, never a parse attempt.
  2. A finish reason that is not complete — truncation, a content filter — is a NoOutput: the bytes that arrived are a prefix, and parsing a prefix is what I18 forbids.
  3. Exactly one tool call: its arguments are the document. The surrounding prose is a preamble and is ignored, because a provider using a function schema as a transport routinely emits both.
  4. More than one tool call: MultipleCandidates. A stage that expects one document never picks one of several.
  5. No tool call: the concatenated text is parsed. A single ```json fence around it is stripped first — a fence is framing a prompt-only transport adds, not content.
§Errors

See StructuredOutputError.

Trait Implementations§

Source§

impl Clone for ModelResponse

Source§

fn clone(&self) -> Self

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 ModelResponse

Source§

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

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

impl<'de> Deserialize<'de> for ModelResponse

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for ModelResponse

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl Serialize for ModelResponse

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for ModelResponse

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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