alexrudall / anthropic

Anthropic (Claude) API + Ruby! 🤖🌌
MIT License
109 stars 28 forks source link

Better handling of parameter errors #17

Open swombat opened 4 months ago

swombat commented 4 months ago

As I tested the new streaming functionality, I found that when the parameters are not quite right it semi-silently fails:

3.3.0 :044 > params
 => 
{:model=>"claude-3-haiku-20240307",
 :temperature=>0.9,
 :max_tokens=>4096,
 :messages=>
  [{:role=>"user",
    :content=>
     "Some kind of preamble that doesn't have arguments.\n\nSample Script Contents 1\n\nAnother section yay\n\nSample Script Contents 1 and Input Item in Special Instruction\n\nThis section has a lot of lines\nIt is here to test the lines.\n\nLet's have some more text:\n\nThis will prevent RuboCop from checking the Sample Script Contents 2 syntax style throughout your entire codebase. Make sure to place this under the correct hierarchy in your .rubocop.yml file, typically at the root level or under a specific target if you have Sample Script Contents 1 configurations for different parts of your project."}],
 :stream=>true,
 :preprocess_stream=>:text} 
3.3.0 :045 > @client.messages(parameters:params)
 => {"type"=>"error", "error"=>{"type"=>"invalid_request_error", "message"=>"preprocess_stream: Extra inputs are not permitted"}} 

I wonder if these kinds of errors (invalid requests in particular) should actually result in an Anthropic::Error being raised.

I can add this code if you think it makes sense.

swombat commented 4 months ago

To be clear this would also happen with other weird mistakes like passing system: nil in the params, which is an easy mistake to make and not a bug in the anthropic gem like the preprocess_stream issue above (which is now fixed in the latest code in my fork).