Skip to content

No exception information returned in Stream mode #256

New issue

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

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

Already on GitHub? # to your account

Closed
lingceng opened this issue May 5, 2023 · 4 comments
Closed

No exception information returned in Stream mode #256

lingceng opened this issue May 5, 2023 · 4 comments

Comments

@lingceng
Copy link

lingceng commented May 5, 2023

The api returns a JSON when error, eg.

{
  "error": {
    "message": "'user_role' is not one of ['system', 'assistant', 'user'] - 'messages.0.role'",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

I use following code to fix

    def self.to_json_stream(user_proc:)

      proc do |chunk, overall_received_bytes, env|
        Rails.logger.debug("raw chunk: #{chunk}")

        if chunk.start_with?("data: {")
          chunk.each_line do |line|
            next unless line.start_with?("data: {")
            line.delete_prefix!("data: ")
            user_proc.call(JSON.parse(line))
          end
        elsif chunk.include?('"error": ')
          user_proc.call(JSON.parse(chunk))
        else
          Rails.logger.warn("unkown raw chunk: #{chunk}")
        end
      end
    end
@lingceng lingceng changed the title No exception information returned in Stream mode when an exception occurs. No exception information returned in Stream mode May 5, 2023
@lingceng
Copy link
Author

The stream data may be split, here is the complete solution: https://gist.github.com/lingceng/afd30be6e102442fa48abf175efe3f6a

Any one can help merge to the project.

@itsderek23 @obie

@harlantwood
Copy link

FYI I have an open PR to solve the same issue, using a different approach: #275

@atesgoral
Copy link
Contributor

I'm working on properly fixing these issues with #338 (and I'll follow up with a breaking change to raise actual exceptions)

@alexrudall
Copy link
Owner

Released in v6

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants