Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chain.stream should return None instead of Error when stream ends #140

Open
prabirshrestha opened this issue May 1, 2024 · 0 comments
Open

Comments

@prabirshrestha
Copy link
Collaborator

Currently I need to write a lot of boilerplate code when the streaming has ended. this also means I need to add async_openai as dependeny.

let mut stream = chain.stream(input_variables).await?;

while let Some(result) = stream.next().await {
    match &result {
        Ok(data) => data.to_stdout()?,
        Err(ChainError::LLMError(LLMError::OpenAIError(
            OpenAIError::StreamError(e),
        ))) => {
            if e == "Stream ended" {
                break;
            } else {
                error!("OpenAI Error: {:?}", e);
                break;
            }
        }
        Err(e) => panic!("Errorx: {:?}", e),
    }
}

This could be simplified to use Result<Option<StreamData>>.

let mut stream = chain.stream(input_variables).await?;

while let Some(result) = stream.next().await {
    match &result {
        Ok(Some(data)) => data.to_stdout()?,
        None => break,
        Err(e) => panic!("Errorx: {:?}", e),
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant