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: RequestIdThe request this answers. Equal to
ModelRequest::request_id
even after a retry.
provider: ProviderKeyWhich provider answered.
model: ModelKeyWhich 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: FinishReasonWhy generation stopped.
usage: TokenUsageReported tokens.
raw_id: Option<String>The provider’s own response identifier, for support tickets. Never a body and never a header.
latency: DurationWall-clock duration of the call, measured by the adapter.
warnings: Vec<ResponseWarning>Adapter warnings.
Implementations§
Source§impl ModelResponse
impl ModelResponse
Sourcepub fn new(
request_id: RequestId,
provider: impl Into<ProviderKey>,
model: impl Into<ModelKey>,
) -> Self
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.
Sourcepub fn with_part(self, part: ContentPart) -> Self
pub fn with_part(self, part: ContentPart) -> Self
Appends a content part.
Sourcepub fn with_tool_call(self, call: ToolCall) -> Self
pub fn with_tool_call(self, call: ToolCall) -> Self
Appends a tool call.
Sourcepub const fn with_finish(self, finish: FinishReason) -> Self
pub const fn with_finish(self, finish: FinishReason) -> Self
Sets the finish reason.
Sourcepub const fn with_usage(self, usage: TokenUsage) -> Self
pub const fn with_usage(self, usage: TokenUsage) -> Self
Sets the reported usage.
Sourcepub fn with_raw_id(self, raw_id: impl Into<String>) -> Self
pub fn with_raw_id(self, raw_id: impl Into<String>) -> Self
Sets the provider’s response identifier.
Sourcepub const fn with_latency(self, latency: Duration) -> Self
pub const fn with_latency(self, latency: Duration) -> Self
Sets the measured latency.
Sourcepub fn with_warning(self, warning: ResponseWarning) -> Self
pub fn with_warning(self, warning: ResponseWarning) -> Self
Adds a warning.
Sourcepub fn text(&self) -> String
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.");Sourcepub fn tool_calls(&self) -> Vec<&ToolCall>
pub fn tool_calls(&self) -> Vec<&ToolCall>
Every tool call, in the order the model produced them.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true when the model produced neither text nor a tool call.
Sourcepub fn single_json(&self) -> Result<Value, StructuredOutputError>
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:
- A
FinishReason::Refusalis aRefusal, never a parse attempt. - 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. - Exactly one tool call: its
argumentsare the document. The surrounding prose is a preamble and is ignored, because a provider using a function schema as a transport routinely emits both. - More than one tool call:
MultipleCandidates. A stage that expects one document never picks one of several. - No tool call: the concatenated text is parsed. A single
```jsonfence around it is stripped first — a fence is framing a prompt-only transport adds, not content.