-
Notifications
You must be signed in to change notification settings - Fork 295
Description
When using a client that has a malformed api_base
is used in a streaming request, the library panics.
This is because in line 413 of the src/client.rs
file, the eventsource function is called, which tries to construct the EventSource
from a failed RequestBuilder
.
The inner value is of type Result::Err
, because in the RequestBuilder
pipeline, the .post
fails (because into_url
is called on the faulty String in the reqwest library), leaving the inner request as an error variant. The .query
, .headers
and .json
functions pass it along, and the try_clone
function bubbles it up.
Because this is then unwrapped in line 414, the entire thread panics.
Minimal reproduction
use async_openai::{Client, config::OpenAIConfig, types::CreateCompletionRequestArgs};
use futures::StreamExt;
#[tokio::main]
async fn main() {
let client = Client::with_config(
OpenAIConfig::new()
.with_api_key("ollama")
.with_api_base("localhost:11434/v1"),
);
let request = CreateCompletionRequestArgs::default()
.model("llama3.2")
.prompt("What is the capital of France?")
.build()
.unwrap();
let mut response = client.completions().create_stream(request).await.unwrap();
while let Some(chunk) = response.next().await {
println!("Received chunk: {:?}", chunk);
}
}
Backtrace:
Note that when not streaming, this caught correctly. However, both completions as well as chats do trigger this.
A solution that doesn't change the type design would be to add a check to the create_stream
function that the into_url
function doesn't fail on the given api base.
Alternatively the URL could be checked when the client is constructed.