1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
use async_trait::async_trait;
use serde::de::DeserializeOwned;
use std::str::FromStr;
use std::time::Duration;
use tracing::{debug, error, info, instrument, trace, warn};
use crate::backend::{
ChatMessage, GenerateResult, LLMClient, MaterializeInternalOutput, MaterializeResult,
ModelInfo, OpenAICompatibleChatCompletionRequest, OpenAICompatibleChatCompletionResponse,
OpenAICompatibleChatMessage, OpenAICompatibleMessageContent, ResponseFormat, ThinkingLevel,
TokenUsage, ValidationFailureContext, check_response_status,
convert_openai_compatible_chat_messages, generate_with_retry_with_history, handle_http_error,
materialize_with_media_with_retry, parse_validate_and_create_output, prepare_strict_schema,
};
use crate::error::{ApiErrorKind, RStructorError, Result};
use crate::model::Instructor;
/// OpenAI models available for completion
///
/// For the latest available models and their identifiers, check the
/// [OpenAI Models Documentation](https://platform.openai.com/docs/models).
///
/// # Using Custom Models
///
/// You can specify any model name as a string using `Custom` variant or `FromStr`:
///
/// ```rust
/// use rstructor::OpenAIModel;
/// use std::str::FromStr;
///
/// // Using Custom variant
/// let model = OpenAIModel::Custom("gpt-4-custom".to_string());
///
/// // Using FromStr (useful for config files)
/// let model = OpenAIModel::from_str("gpt-4-custom").unwrap();
///
/// // Or use the convenience method
/// let model = OpenAIModel::from_string("gpt-4-custom");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Model {
/// GPT-5.5 Pro (most capable GPT-5.5 model)
Gpt55Pro,
/// GPT-5.5 (latest frontier model for complex professional work)
Gpt55,
/// GPT-5.4 (more affordable frontier model for complex professional work)
Gpt54,
/// GPT-5.4 Mini (lower-latency, lower-cost GPT-5.4-class model)
Gpt54Mini,
/// GPT-5.4 Nano (cheapest GPT-5.4-class model for high-volume tasks)
Gpt54Nano,
/// GPT-5.2 Pro (previous GPT-5.2 pro model)
Gpt52Pro,
/// GPT-5.2 (previous GPT-5.2 model)
Gpt52,
/// GPT-5.2 Chat Latest (ChatGPT GPT-5.2 model)
Gpt52ChatLatest,
/// GPT-5.2 Codex (coding-focused GPT-5.2 model)
Gpt52Codex,
/// GPT-5.1 (GPT-5.1 model)
Gpt51,
/// GPT-5 Chat Latest (ChatGPT GPT-5 model)
Gpt5ChatLatest,
/// GPT-5 Pro (most capable GPT-5 model)
Gpt5Pro,
/// GPT-5 (standard GPT-5 model)
Gpt5,
/// GPT-5 Nano (smallest GPT-5 model)
Gpt5Nano,
/// GPT-5 Mini (smaller, faster GPT-5 model)
Gpt5Mini,
/// GPT-4.1 (GPT-4.1 model)
Gpt41,
/// GPT-4.1 Mini (smaller GPT-4.1)
Gpt41Mini,
/// GPT-4.1 Nano (smallest GPT-4.1)
Gpt41Nano,
/// GPT-4o (previous GPT-4o model, optimized for chat)
Gpt4O,
/// GPT-4o Mini (smaller, faster, more cost-effective version)
Gpt4OMini,
/// GPT-4 Turbo (high-intelligence model)
Gpt4Turbo,
/// GPT-4 (standard GPT-4 model)
Gpt4,
/// GPT-3.5 Turbo (efficient model for simple tasks)
Gpt35Turbo,
/// O4 Mini (previous small reasoning model)
O4Mini,
/// O3 (reasoning model)
O3,
/// O3 Mini (smaller reasoning model)
O3Mini,
/// O1 (reasoning model optimized for complex problem-solving)
O1,
/// O1 Mini (smaller reasoning model)
O1Mini,
/// O1 Pro (most capable reasoning model)
O1Pro,
/// Custom model name (for new models, local LLMs, or OpenAI-compatible endpoints)
Custom(String),
}
impl Model {
pub fn as_str(&self) -> &str {
match self {
Model::Gpt55Pro => "gpt-5.5-pro",
Model::Gpt55 => "gpt-5.5",
Model::Gpt54 => "gpt-5.4",
Model::Gpt54Mini => "gpt-5.4-mini",
Model::Gpt54Nano => "gpt-5.4-nano",
Model::Gpt52Pro => "gpt-5.2-pro",
Model::Gpt52 => "gpt-5.2",
Model::Gpt52ChatLatest => "gpt-5.2-chat-latest",
Model::Gpt52Codex => "gpt-5.2-codex",
Model::Gpt51 => "gpt-5.1",
Model::Gpt5ChatLatest => "gpt-5-chat-latest",
Model::Gpt5Pro => "gpt-5-pro",
Model::Gpt5 => "gpt-5",
Model::Gpt5Nano => "gpt-5-nano",
Model::Gpt5Mini => "gpt-5-mini",
Model::Gpt41 => "gpt-4.1",
Model::Gpt41Mini => "gpt-4.1-mini",
Model::Gpt41Nano => "gpt-4.1-nano",
Model::Gpt4O => "gpt-4o",
Model::Gpt4OMini => "gpt-4o-mini",
Model::Gpt4Turbo => "gpt-4-turbo",
Model::Gpt4 => "gpt-4",
Model::Gpt35Turbo => "gpt-3.5-turbo",
Model::O4Mini => "o4-mini",
Model::O3 => "o3",
Model::O3Mini => "o3-mini",
Model::O1 => "o1",
Model::O1Mini => "o1-mini",
Model::O1Pro => "o1-pro",
Model::Custom(name) => name,
}
}
/// Create a model from a string. This is a convenience method that always succeeds.
///
/// If the string matches a known model variant, it returns that variant.
/// Otherwise, it returns `Custom(name)`.
pub fn from_string(name: impl Into<String>) -> Self {
let name = name.into();
match name.as_str() {
"gpt-5.5-pro" => Model::Gpt55Pro,
"gpt-5.5" => Model::Gpt55,
"gpt-5.4" => Model::Gpt54,
"gpt-5.4-mini" => Model::Gpt54Mini,
"gpt-5.4-nano" => Model::Gpt54Nano,
"gpt-5.2-pro" => Model::Gpt52Pro,
"gpt-5.2" => Model::Gpt52,
"gpt-5.2-chat-latest" => Model::Gpt52ChatLatest,
"gpt-5.2-codex" => Model::Gpt52Codex,
"gpt-5.1" => Model::Gpt51,
"gpt-5-chat-latest" => Model::Gpt5ChatLatest,
"gpt-5-pro" => Model::Gpt5Pro,
"gpt-5" => Model::Gpt5,
"gpt-5-nano" => Model::Gpt5Nano,
"gpt-5-mini" => Model::Gpt5Mini,
"gpt-4.1" => Model::Gpt41,
"gpt-4.1-mini" => Model::Gpt41Mini,
"gpt-4.1-nano" => Model::Gpt41Nano,
"gpt-4o" => Model::Gpt4O,
"gpt-4o-mini" => Model::Gpt4OMini,
"gpt-4-turbo" => Model::Gpt4Turbo,
"gpt-4" => Model::Gpt4,
"gpt-3.5-turbo" => Model::Gpt35Turbo,
"o4-mini" => Model::O4Mini,
"o3" => Model::O3,
"o3-mini" => Model::O3Mini,
"o1" => Model::O1,
"o1-mini" => Model::O1Mini,
"o1-pro" => Model::O1Pro,
_ => Model::Custom(name),
}
}
}
impl FromStr for Model {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(Model::from_string(s))
}
}
impl From<&str> for Model {
fn from(s: &str) -> Self {
Model::from_string(s)
}
}
impl From<String> for Model {
fn from(s: String) -> Self {
Model::from_string(s)
}
}
/// Configuration for the OpenAI client
#[derive(Debug, Clone)]
pub struct OpenAIConfig {
pub api_key: String,
pub model: Model,
pub temperature: f32,
pub max_tokens: Option<u32>,
pub timeout: Option<Duration>,
pub max_retries: Option<usize>,
/// Custom base URL for OpenAI-compatible APIs (e.g., local LLMs, proxy endpoints)
/// Defaults to "https://api.openai.com/v1" if not set
pub base_url: Option<String>,
/// Thinking level for GPT-5.x models (reasoning effort)
/// Controls the depth of reasoning applied to prompts
pub thinking_level: Option<ThinkingLevel>,
}
/// OpenAI client for generating completions
#[derive(Clone)]
pub struct OpenAIClient {
config: OpenAIConfig,
client: reqwest::Client,
}
// ResponseFormat and JsonSchemaFormat are imported from utils and shared
// OpenAI-compatible chat completion request/response types are in openai_compatible.rs.
impl OpenAIClient {
/// Create a new OpenAI client with the provided API key.
///
/// # Arguments
///
/// * `api_key` - Your OpenAI API key
///
/// # Examples
///
/// ```no_run
/// # use rstructor::OpenAIClient;
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = OpenAIClient::new("your-openai-api-key")?;
/// # Ok(())
/// # }
/// ```
#[instrument(name = "openai_client_new", skip(api_key), fields(model = ?Model::Gpt55))]
pub fn new(api_key: impl Into<String>) -> Result<Self> {
let api_key = api_key.into();
if api_key.is_empty() {
return Err(RStructorError::api_error(
"OpenAI",
ApiErrorKind::AuthenticationFailed,
));
}
info!("Creating new OpenAI client");
trace!("API key length: {}", api_key.len());
let config = OpenAIConfig {
api_key,
model: Model::Gpt55, // Default to GPT-5.5 (latest frontier model)
temperature: 0.0,
max_tokens: None,
timeout: None, // Default: no timeout (uses reqwest's default)
max_retries: Some(3), // Default: 3 retries with error feedback
base_url: None, // Default: use official OpenAI API
thinking_level: Some(ThinkingLevel::Medium), // GPT-5.5 defaults to medium reasoning
};
debug!("OpenAI client created with default configuration");
Ok(Self {
config,
client: reqwest::Client::new(),
})
}
/// Create a new OpenAI client by reading the API key from the `OPENAI_API_KEY` environment variable.
///
/// # Errors
///
/// Returns an error if `OPENAI_API_KEY` is not set.
///
/// # Examples
///
/// ```no_run
/// # use rstructor::OpenAIClient;
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = OpenAIClient::from_env()?;
/// # Ok(())
/// # }
/// ```
#[instrument(name = "openai_client_from_env", fields(model = ?Model::Gpt55))]
pub fn from_env() -> Result<Self> {
let api_key = std::env::var("OPENAI_API_KEY")
.map_err(|_| RStructorError::api_error("OpenAI", ApiErrorKind::AuthenticationFailed))?;
info!("Creating new OpenAI client from environment variable");
trace!("API key length: {}", api_key.len());
let config = OpenAIConfig {
api_key,
model: Model::Gpt55, // Default to GPT-5.5 (latest frontier model)
temperature: 0.0,
max_tokens: None,
timeout: None, // Default: no timeout (uses reqwest's default)
max_retries: Some(3), // Default: 3 retries with error feedback
base_url: None, // Default: use official OpenAI API
thinking_level: Some(ThinkingLevel::Medium), // GPT-5.5 defaults to medium reasoning
};
debug!("OpenAI client created with default configuration");
Ok(Self {
config,
client: reqwest::Client::new(),
})
}
// Builder methods are generated by the macro below
}
// Generate builder methods using macro
crate::impl_client_builder_methods! {
client_type: OpenAIClient,
config_type: OpenAIConfig,
model_type: Model,
provider_name: "OpenAI"
}
impl OpenAIClient {
/// Set a custom base URL for OpenAI-compatible APIs (e.g., local LLMs, proxy endpoints).
///
/// # Arguments
///
/// * `base_url` - Base URL without trailing slash (e.g., "http://localhost:1234/v1" or "https://api.example.com/v1")
///
/// # Example
///
/// ```rust,no_run
/// use rstructor::OpenAIClient;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = OpenAIClient::new("api-key")?
/// .base_url("http://localhost:1234/v1")
/// .model("llama-3.1-70b");
/// # Ok(())
/// # }
/// ```
#[tracing::instrument(skip(self, base_url))]
pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
let base_url_str = base_url.into();
tracing::debug!(
previous_base_url = ?self.config.base_url,
new_base_url = %base_url_str,
"Setting custom base URL"
);
self.config.base_url = Some(base_url_str);
self
}
/// Set the thinking level for GPT-5.x models (reasoning effort).
///
/// Controls the depth of reasoning the model applies to prompts.
/// Higher thinking levels provide deeper reasoning but increase latency and cost.
///
/// Note: When reasoning is enabled (any level except `Off`), temperature is
/// automatically set to 1.0 as required by the API.
///
/// # Reasoning Effort Levels
///
/// - `Off`: No extended reasoning (maps to "none")
/// - `Minimal`: Light reasoning (maps to "low")
/// - `Low`: Standard reasoning (maps to "low", default)
/// - `Medium`: Balanced reasoning (maps to "medium")
/// - `High`: Deep reasoning (maps to "high")
///
/// # Example
///
/// ```rust,no_run
/// use rstructor::{OpenAIClient, ThinkingLevel};
///
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = OpenAIClient::from_env()?
/// .thinking_level(ThinkingLevel::High);
/// # Ok(())
/// # }
/// ```
#[tracing::instrument(skip(self))]
pub fn thinking_level(mut self, level: ThinkingLevel) -> Self {
tracing::debug!(
previous_level = ?self.config.thinking_level,
new_level = ?level,
"Setting thinking level"
);
self.config.thinking_level = Some(level);
self
}
/// Internal implementation of materialize (without retry logic)
/// Accepts conversation history for multi-turn interactions.
/// Returns the data, raw response, and optional usage info.
///
/// Uses OpenAI's native Structured Outputs with `response_format: json_schema`
/// for guaranteed schema compliance.
///
/// The raw response is included to enable conversation history tracking for retries,
/// which improves prompt caching efficiency.
async fn materialize_internal<T>(
&self,
messages: &[ChatMessage],
) -> std::result::Result<
MaterializeInternalOutput<T>,
(RStructorError, Option<ValidationFailureContext>),
>
where
T: Instructor + DeserializeOwned + Send + 'static,
{
info!("Generating structured response with OpenAI (native structured outputs)");
// Get the schema for type T
let schema = T::schema();
let schema_name = T::schema_name().unwrap_or_else(|| "output".to_string());
// Avoid calling to_string() in trace to prevent potential stack overflow with complex schemas
trace!(schema_name = schema_name, "Retrieved JSON schema for type");
// Prepare schema with additionalProperties: false recursively for all nested objects
let schema_json = prepare_strict_schema(&schema);
// Create response format with JSON schema (strict mode)
let response_format = ResponseFormat::json_schema(
schema_name.clone(),
schema_json,
Some("Output in the specified format. Include ALL required fields and follow the schema exactly.".to_string()),
);
// Build reasoning_effort for GPT-5.x models
let is_gpt5 = self.config.model.as_str().starts_with("gpt-5");
let reasoning_effort = if is_gpt5 {
self.config
.thinking_level
.and_then(|level| level.openai_reasoning_effort().map(|s| s.to_string()))
} else {
None
};
// GPT-5.x with reasoning requires temperature=1.0
let effective_temp = if reasoning_effort.is_some() {
1.0
} else {
self.config.temperature
};
// Convert ChatMessage to OpenAI's format
let api_messages =
convert_openai_compatible_chat_messages(messages, "OpenAI").map_err(|e| (e, None))?;
// Build the request with native structured outputs
debug!(
"Building OpenAI API request with structured outputs (history_len={})",
api_messages.len()
);
let request = OpenAICompatibleChatCompletionRequest {
model: self.config.model.as_str().to_string(),
messages: api_messages,
response_format: Some(response_format),
temperature: effective_temp,
max_tokens: self.config.max_tokens,
reasoning_effort,
};
// Send the request to OpenAI
let base_url = self
.config
.base_url
.as_deref()
.unwrap_or("https://api.openai.com/v1");
let url = format!("{}/chat/completions", base_url);
debug!(url = %url, "Sending request to OpenAI API");
let response = self
.client
.post(&url)
.header("Authorization", format!("Bearer {}", self.config.api_key))
.header("Content-Type", "application/json")
.json(&request)
.send()
.await
.map_err(|e| (handle_http_error(e, "OpenAI"), None))?;
// Parse the response
let response = check_response_status(response, "OpenAI")
.await
.map_err(|e| (e, None))?;
debug!("Successfully received response from OpenAI");
let completion: OpenAICompatibleChatCompletionResponse =
response.json().await.map_err(|e| {
error!(error = %e, "Failed to parse JSON response from OpenAI");
(RStructorError::from(e), None)
})?;
if completion.choices.is_empty() {
error!("OpenAI returned empty choices array");
return Err((
RStructorError::api_error(
"OpenAI",
ApiErrorKind::UnexpectedResponse {
details: "No completion choices returned".to_string(),
},
),
None,
));
}
// Extract usage info
let model_name = completion
.model
.clone()
.unwrap_or_else(|| self.config.model.as_str().to_string());
let usage = completion
.usage
.as_ref()
.map(|u| TokenUsage::new(model_name.clone(), u.prompt_tokens, u.completion_tokens));
let message = &completion.choices[0].message;
trace!(finish_reason = %completion.choices[0].finish_reason, "Completion finish reason");
// With structured outputs, the response is in message.content as guaranteed-valid JSON
if let Some(content) = &message.content {
let raw_response = content.clone();
debug!(
content_len = raw_response.len(),
"Structured output received from OpenAI"
);
// Parse and validate the response using shared utility
parse_validate_and_create_output(raw_response, usage)
} else {
error!("No content in OpenAI response");
Err((
RStructorError::api_error(
"OpenAI",
ApiErrorKind::UnexpectedResponse {
details: "No content in response".to_string(),
},
),
None,
))
}
}
}
#[async_trait]
impl LLMClient for OpenAIClient {
fn from_env() -> Result<Self> {
Self::from_env()
}
#[instrument(
name = "openai_materialize",
skip(self, prompt),
fields(
type_name = std::any::type_name::<T>(),
model = %self.config.model.as_str(),
prompt_len = prompt.len()
)
)]
async fn materialize<T>(&self, prompt: &str) -> Result<T>
where
T: Instructor + DeserializeOwned + Send + 'static,
{
let output = generate_with_retry_with_history(
|messages: Vec<ChatMessage>| {
let this = self;
async move { this.materialize_internal::<T>(&messages).await }
},
prompt,
self.config.max_retries,
)
.await?;
Ok(output.data)
}
#[instrument(
name = "openai_materialize_with_media",
skip(self, prompt, media),
fields(
type_name = std::any::type_name::<T>(),
model = %self.config.model.as_str(),
prompt_len = prompt.len(),
media_len = media.len()
)
)]
async fn materialize_with_media<T>(&self, prompt: &str, media: &[super::MediaFile]) -> Result<T>
where
T: Instructor + DeserializeOwned + Send + 'static,
{
materialize_with_media_with_retry(
|messages: Vec<ChatMessage>| {
let this = self;
async move { this.materialize_internal::<T>(&messages).await }
},
prompt,
media,
self.config.max_retries,
)
.await
}
#[instrument(
name = "openai_materialize_with_metadata",
skip(self, prompt),
fields(
type_name = std::any::type_name::<T>(),
model = %self.config.model.as_str(),
prompt_len = prompt.len()
)
)]
async fn materialize_with_metadata<T>(&self, prompt: &str) -> Result<MaterializeResult<T>>
where
T: Instructor + DeserializeOwned + Send + 'static,
{
let output = generate_with_retry_with_history(
|messages: Vec<ChatMessage>| {
let this = self;
async move { this.materialize_internal::<T>(&messages).await }
},
prompt,
self.config.max_retries,
)
.await?;
Ok(MaterializeResult::new(output.data, output.usage))
}
#[instrument(
name = "openai_generate",
skip(self, prompt),
fields(
model = %self.config.model.as_str(),
prompt_len = prompt.len()
)
)]
async fn generate(&self, prompt: &str) -> Result<String> {
let result = self.generate_with_metadata(prompt).await?;
Ok(result.text)
}
#[instrument(
name = "openai_generate_with_metadata",
skip(self, prompt),
fields(
model = %self.config.model.as_str(),
prompt_len = prompt.len()
)
)]
async fn generate_with_metadata(&self, prompt: &str) -> Result<GenerateResult> {
info!("Generating raw text response with OpenAI");
// Build reasoning_effort for GPT-5.x models
let is_gpt5 = self.config.model.as_str().starts_with("gpt-5");
let reasoning_effort = if is_gpt5 {
self.config
.thinking_level
.and_then(|level| level.openai_reasoning_effort().map(|s| s.to_string()))
} else {
None
};
// GPT-5.x with reasoning requires temperature=1.0
let effective_temp = if reasoning_effort.is_some() {
1.0
} else {
self.config.temperature
};
// Build the request for text generation (no structured output)
debug!("Building OpenAI API request for text generation");
let request = OpenAICompatibleChatCompletionRequest {
model: self.config.model.as_str().to_string(),
messages: vec![OpenAICompatibleChatMessage {
role: "user".to_string(),
content: OpenAICompatibleMessageContent::Text(prompt.to_string()),
}],
response_format: None,
temperature: effective_temp,
max_tokens: self.config.max_tokens,
reasoning_effort,
};
// Send the request to OpenAI
let base_url = self
.config
.base_url
.as_deref()
.unwrap_or("https://api.openai.com/v1");
let url = format!("{}/chat/completions", base_url);
debug!(url = %url, "Sending request to OpenAI API");
let response = self
.client
.post(&url)
.header("Authorization", format!("Bearer {}", self.config.api_key))
.header("Content-Type", "application/json")
.json(&request)
.send()
.await
.map_err(|e| handle_http_error(e, "OpenAI"))?;
// Parse the response
let response = check_response_status(response, "OpenAI").await?;
debug!("Successfully received response from OpenAI");
let completion: OpenAICompatibleChatCompletionResponse =
response.json().await.map_err(|e| {
error!(error = %e, "Failed to parse JSON response from OpenAI");
e
})?;
if completion.choices.is_empty() {
error!("OpenAI returned empty choices array");
return Err(RStructorError::api_error(
"OpenAI",
ApiErrorKind::UnexpectedResponse {
details: "No completion choices returned".to_string(),
},
));
}
// Extract usage info
let model_name = completion
.model
.clone()
.unwrap_or_else(|| self.config.model.as_str().to_string());
let usage = completion
.usage
.as_ref()
.map(|u| TokenUsage::new(model_name, u.prompt_tokens, u.completion_tokens));
let message = &completion.choices[0].message;
trace!(finish_reason = %completion.choices[0].finish_reason, "Completion finish reason");
if let Some(content) = &message.content {
debug!(
content_len = content.len(),
"Successfully extracted content from response"
);
Ok(GenerateResult::new(content.clone(), usage))
} else {
error!("No content in OpenAI response");
Err(RStructorError::api_error(
"OpenAI",
ApiErrorKind::UnexpectedResponse {
details: "No content in response".to_string(),
},
))
}
}
/// Fetch available models from OpenAI's API.
///
/// Returns a list of GPT models available for chat completions.
/// Filters out embedding, whisper, and other non-chat models.
async fn list_models(&self) -> Result<Vec<ModelInfo>> {
let base_url = self
.config
.base_url
.as_deref()
.unwrap_or("https://api.openai.com/v1");
let url = format!("{}/models", base_url);
debug!(url = %url, "Fetching available models from OpenAI");
let response = self
.client
.get(&url)
.header("Authorization", format!("Bearer {}", self.config.api_key))
.header("Content-Type", "application/json")
.send()
.await
.map_err(|e| handle_http_error(e, "OpenAI"))?;
let response = check_response_status(response, "OpenAI").await?;
let json: serde_json::Value = response.json().await.map_err(|e| {
error!(error = %e, "Failed to parse models response from OpenAI");
e
})?;
let models = json
.get("data")
.and_then(|data| data.as_array())
.map(|models_array| {
models_array
.iter()
.filter_map(|model| {
let id = model.get("id").and_then(|id| id.as_str())?;
// Filter to only GPT models (chat completion models)
if id.starts_with("gpt-")
|| id.starts_with("o1")
|| id.starts_with("o3")
|| id.starts_with("o4")
{
Some(ModelInfo {
id: id.to_string(),
name: None,
description: None,
})
} else {
None
}
})
.collect::<Vec<_>>()
})
.unwrap_or_default();
debug!(count = models.len(), "Fetched OpenAI models");
Ok(models)
}
}