dynamo_async_openai/types/
step.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Based on https://github.com/64bit/async-openai/ by Himanshu Neema
5// Original Copyright (c) 2022 Himanshu Neema
6// Licensed under MIT License (see ATTRIBUTIONS-Rust.md)
7//
8// Modifications Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
9// Licensed under Apache 2.0
10
11use std::collections::HashMap;
12
13use serde::{Deserialize, Serialize};
14
15use super::{FileSearchRankingOptions, ImageFile, LastError, RunStatus};
16
17#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
18#[serde(rename_all = "snake_case")]
19pub enum RunStepType {
20    MessageCreation,
21    ToolCalls,
22}
23
24/// Represents a step in execution of a run.
25#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
26pub struct RunStepObject {
27    /// The identifier, which can be referenced in API endpoints.
28    pub id: String,
29    /// The object type, which is always `thread.run.step`.
30    pub object: String,
31    /// The Unix timestamp (in seconds) for when the run step was created.
32    pub created_at: i32,
33
34    /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) associated with the run step.
35    pub assistant_id: Option<String>,
36
37    /// The ID of the [thread](https://platform.openai.com/docs/api-reference/threads) that was run.
38    pub thread_id: String,
39
40    ///  The ID of the [run](https://platform.openai.com/docs/api-reference/runs) that this run step is a part of.
41    pub run_id: String,
42
43    /// The type of run step, which can be either `message_creation` or `tool_calls`.
44    pub r#type: RunStepType,
45
46    /// The status of the run step, which can be either `in_progress`, `cancelled`, `failed`, `completed`, or `expired`.
47    pub status: RunStatus,
48
49    /// The details of the run step.
50    pub step_details: StepDetails,
51
52    /// The last error associated with this run. Will be `null` if there are no errors.
53    pub last_error: Option<LastError>,
54
55    ///The Unix timestamp (in seconds) for when the run step expired. A step is considered expired if the parent run is expired.
56    pub expires_at: Option<i32>,
57
58    /// The Unix timestamp (in seconds) for when the run step was cancelled.
59    pub cancelled_at: Option<i32>,
60
61    /// The Unix timestamp (in seconds) for when the run step failed.
62    pub failed_at: Option<i32>,
63
64    /// The Unix timestamp (in seconds) for when the run step completed.
65    pub completed_at: Option<i32>,
66
67    pub metadata: Option<HashMap<String, serde_json::Value>>,
68
69    /// Usage statistics related to the run step. This value will be `null` while the run step's status is `in_progress`.
70    pub usage: Option<RunStepCompletionUsage>,
71}
72
73#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
74pub struct RunStepCompletionUsage {
75    /// Number of completion tokens used over the course of the run step.
76    pub completion_tokens: u32,
77    /// Number of prompt tokens used over the course of the run step.
78    pub prompt_tokens: u32,
79    /// Total number of tokens used (prompt + completion).
80    pub total_tokens: u32,
81}
82
83#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
84#[serde(tag = "type")]
85#[serde(rename_all = "snake_case")]
86pub enum StepDetails {
87    MessageCreation(RunStepDetailsMessageCreationObject),
88    ToolCalls(RunStepDetailsToolCallsObject),
89}
90
91/// Details of the message creation by the run step.
92#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
93pub struct RunStepDetailsMessageCreationObject {
94    pub message_creation: MessageCreation,
95}
96
97#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
98pub struct MessageCreation {
99    /// The ID of the message that was created by this run step.
100    pub message_id: String,
101}
102
103/// Details of the tool call.
104#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
105pub struct RunStepDetailsToolCallsObject {
106    /// An array of tool calls the run step was involved in. These can be associated with one of three types of tools: `code_interpreter`, `file_search`, or `function`.
107    pub tool_calls: Vec<RunStepDetailsToolCalls>,
108}
109
110#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
111#[serde(tag = "type")]
112#[serde(rename_all = "snake_case")]
113pub enum RunStepDetailsToolCalls {
114    /// Details of the Code Interpreter tool call the run step was involved in.
115    CodeInterpreter(RunStepDetailsToolCallsCodeObject),
116    FileSearch(RunStepDetailsToolCallsFileSearchObject),
117    Function(RunStepDetailsToolCallsFunctionObject),
118}
119
120/// Code interpreter tool call
121#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
122pub struct RunStepDetailsToolCallsCodeObject {
123    /// The ID of the tool call.
124    pub id: String,
125
126    /// The Code Interpreter tool call definition.
127    pub code_interpreter: CodeInterpreter,
128}
129
130#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
131pub struct CodeInterpreter {
132    /// The input to the Code Interpreter tool call.
133    pub input: String,
134    /// The outputs from the Code Interpreter tool call. Code Interpreter can output one or more items, including text (`logs`) or images (`image`). Each of these are represented by a different object type.
135    pub outputs: Vec<CodeInterpreterOutput>,
136}
137
138#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
139#[serde(tag = "type")]
140#[serde(rename_all = "lowercase")]
141pub enum CodeInterpreterOutput {
142    /// Code interpreter log output
143    Logs(RunStepDetailsToolCallsCodeOutputLogsObject),
144    /// Code interpreter image output
145    Image(RunStepDetailsToolCallsCodeOutputImageObject),
146}
147
148/// Text output from the Code Interpreter tool call as part of a run step.
149#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
150pub struct RunStepDetailsToolCallsCodeOutputLogsObject {
151    /// The text output from the Code Interpreter tool call.
152    pub logs: String,
153}
154
155#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
156pub struct RunStepDetailsToolCallsCodeOutputImageObject {
157    /// The [file](https://platform.openai.com/docs/api-reference/files) ID of the image.
158    pub image: ImageFile,
159}
160
161/// File search tool call
162#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
163pub struct RunStepDetailsToolCallsFileSearchObject {
164    /// The ID of the tool call object.
165    pub id: String,
166    /// For now, this is always going to be an empty object.
167    pub file_search: RunStepDetailsToolCallsFileSearchObjectFileSearch,
168}
169
170#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
171pub struct RunStepDetailsToolCallsFileSearchObjectFileSearch {
172    pub ranking_options: Option<FileSearchRankingOptions>,
173    /// The results of the file search.
174    pub results: Option<Vec<RunStepDetailsToolCallsFileSearchResultObject>>,
175}
176
177/// A result instance of the file search.
178#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
179pub struct RunStepDetailsToolCallsFileSearchResultObject {
180    /// The ID of the file that result was found in.
181    pub file_id: String,
182    /// The name of the file that result was found in.
183    pub file_name: String,
184    /// The score of the result. All values must be a floating point number between 0 and 1.
185    pub score: f32,
186    /// The content of the result that was found. The content is only included if requested via the include query parameter.
187    pub content: Option<Vec<RunStepDetailsToolCallsFileSearchResultObjectContent>>,
188}
189
190#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
191pub struct RunStepDetailsToolCallsFileSearchResultObjectContent {
192    // note: type is text hence omitted from struct
193    /// The text content of the file.
194    pub text: Option<String>,
195}
196
197#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
198pub struct RunStepDetailsToolCallsFunctionObject {
199    /// The ID of the tool call object.
200    pub id: String,
201    /// he definition of the function that was called.
202    pub function: RunStepFunctionObject,
203}
204
205#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
206pub struct RunStepFunctionObject {
207    /// The name of the function.
208    pub name: String,
209    /// The arguments passed to the function.
210    pub arguments: String,
211    /// The output of the function. This will be `null` if the outputs have not been [submitted](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) yet.
212    pub output: Option<String>,
213}
214
215#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
216pub struct RunStepFunctionObjectDelta {
217    /// The name of the function.
218    pub name: Option<String>,
219    /// The arguments passed to the function.
220    pub arguments: Option<String>,
221    /// The output of the function. This will be `null` if the outputs have not been [submitted](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) yet.
222    pub output: Option<String>,
223}
224
225#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
226pub struct ListRunStepsResponse {
227    pub object: String,
228    pub data: Vec<RunStepObject>,
229    pub first_id: Option<String>,
230    pub last_id: Option<String>,
231    pub has_more: bool,
232}
233
234/// Represents a run step delta i.e. any changed fields on a run step during streaming.
235#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
236pub struct RunStepDeltaObject {
237    /// The identifier of the run step, which can be referenced in API endpoints.
238    pub id: String,
239    /// The object type, which is always `thread.run.step.delta`.
240    pub object: String,
241    /// The delta containing the fields that have changed on the run step.
242    pub delta: RunStepDelta,
243}
244
245#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
246pub struct RunStepDelta {
247    pub step_details: DeltaStepDetails,
248}
249
250#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
251#[serde(tag = "type")]
252#[serde(rename_all = "snake_case")]
253pub enum DeltaStepDetails {
254    MessageCreation(RunStepDeltaStepDetailsMessageCreationObject),
255    ToolCalls(RunStepDeltaStepDetailsToolCallsObject),
256}
257
258/// Details of the message creation by the run step.
259#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
260pub struct RunStepDeltaStepDetailsMessageCreationObject {
261    pub message_creation: Option<MessageCreation>,
262}
263
264/// Details of the tool call.
265#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
266pub struct RunStepDeltaStepDetailsToolCallsObject {
267    /// An array of tool calls the run step was involved in. These can be associated with one of three types of tools: `code_interpreter`, `file_search`, or `function`.
268    pub tool_calls: Option<Vec<RunStepDeltaStepDetailsToolCalls>>,
269}
270
271#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
272#[serde(tag = "type")]
273#[serde(rename_all = "snake_case")]
274pub enum RunStepDeltaStepDetailsToolCalls {
275    CodeInterpreter(RunStepDeltaStepDetailsToolCallsCodeObject),
276    FileSearch(RunStepDeltaStepDetailsToolCallsFileSearchObject),
277    Function(RunStepDeltaStepDetailsToolCallsFunctionObject),
278}
279
280/// Details of the Code Interpreter tool call the run step was involved in.
281#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
282pub struct RunStepDeltaStepDetailsToolCallsCodeObject {
283    /// The index of the tool call in the tool calls array.
284    pub index: u32,
285    /// The ID of the tool call.
286    pub id: Option<String>,
287    /// The Code Interpreter tool call definition.
288    pub code_interpreter: Option<DeltaCodeInterpreter>,
289}
290
291#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
292pub struct DeltaCodeInterpreter {
293    /// The input to the Code Interpreter tool call.
294    pub input: Option<String>,
295    /// The outputs from the Code Interpreter tool call. Code Interpreter can output one or more items, including text (`logs`) or images (`image`). Each of these are represented by a different object type.
296    pub outputs: Option<Vec<DeltaCodeInterpreterOutput>>,
297}
298
299#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
300#[serde(tag = "type")]
301#[serde(rename_all = "lowercase")]
302pub enum DeltaCodeInterpreterOutput {
303    Logs(RunStepDeltaStepDetailsToolCallsCodeOutputLogsObject),
304    Image(RunStepDeltaStepDetailsToolCallsCodeOutputImageObject),
305}
306
307/// Text output from the Code Interpreter tool call as part of a run step.
308#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
309pub struct RunStepDeltaStepDetailsToolCallsCodeOutputLogsObject {
310    /// The index of the output in the outputs array.
311    pub index: u32,
312    /// The text output from the Code Interpreter tool call.
313    pub logs: Option<String>,
314}
315
316/// Code interpreter image output
317#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
318pub struct RunStepDeltaStepDetailsToolCallsCodeOutputImageObject {
319    /// The index of the output in the outputs array.
320    pub index: u32,
321
322    pub image: Option<ImageFile>,
323}
324
325#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
326pub struct RunStepDeltaStepDetailsToolCallsFileSearchObject {
327    /// The index of the tool call in the tool calls array.
328    pub index: u32,
329    /// The ID of the tool call object.
330    pub id: Option<String>,
331    /// For now, this is always going to be an empty object.
332    pub file_search: Option<serde_json::Value>,
333}
334
335/// Function tool call
336#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
337pub struct RunStepDeltaStepDetailsToolCallsFunctionObject {
338    /// The index of the tool call in the tool calls array.
339    pub index: u32,
340    /// The ID of the tool call object.
341    pub id: Option<String>,
342    /// The definition of the function that was called.
343    pub function: Option<RunStepFunctionObjectDelta>,
344}