Which of these :ok and :error are more idiomatic/better?

I’m abstracting OpenAI’s Chat Completion API. The API converts text input into text output. For a successful response, the http status code is 200 with either finish_reason=“stop” or finish_reason=“length” and the output. “stop” means the output is complete. “length” means the output is partial, so we should concatenate the output to the input and re-send the request.

It seemed like an obvious choice to translate status != “200” to {:error, reason}, but I’m wavering on the example I outlined above.

What’s more Elixir?

{:ok, :stop, output}
{:ok, :length, output}

or

{:ok, output} # means "stop"
{:error, :length, output}

EDIT: writing this out makes me feel that :error, :length, output is better, since it’s an unexpected result, and :error calls more attention to it. WDYT?

My opinionated view it to go with something like:

{:ok, output}
{:partial, output}

Both results are successful so I don’t think :error is quite right. And there’s no particular magic about needing to use :ok and :error. Use something that reflects the domain and it something you’ll remember in a couple of years time.

5 Likes

I like {:partial, output}, thanks!

2 Likes