Fix bang methods to return naked values and streamline documentation#9
Conversation
- Updated examples in README.md, usage-rules.md, and guides to remove the use of `{:ok, result}` tuples in favor of direct returns from `generate_text!/3`, `stream_text!/3`, and `generate_object!/3` functions.
- This change enhances readability and aligns the documentation with the current API behavior, ensuring users have a clear understanding of the expected outputs.
- Updated stream_text mix task to handle bang methods returning streams directly - Changed from case pattern matching to try/rescue for error handling - Fixed type spec to include no_return() for System.halt calls - Formatted code to pass quality checks Amp-Thread-ID: https://ampcode.com/threads/T-30e4888c-d2e0-4653-a5fe-7995eff0fc96 Co-authored-by: Amp <amp@ampcode.com>
Pull Request Test Coverage Report for Build 20c5d904b413fefeff5b27db94af2ff51526bb73-PR-9Details
💛 - Coveralls |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes bang method behavior to follow idiomatic Elixir conventions and streamlines documentation examples. The key change is making bang methods return naked values and raise exceptions instead of returning tuples.
- Updated all bang methods to return unwrapped values and raise on error instead of returning {:ok, value} or {:error, term} tuples
- Streamlined documentation examples by removing unnecessary intermediate variable assignments
- Updated type specifications to reflect the new behavior with no_return() for error cases
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/req_llm.ex | Updated bang method documentation examples and added "Raises on error" notes |
| lib/req_llm/generation.ex | Fixed bang method implementations, type specs, and documentation examples |
| lib/mix/tasks/stream_text.ex | Updated to handle new bang method behavior with try/rescue pattern |
| usage-rules.md | Simplified documentation example by removing intermediate variable |
| guides/getting-started.md | Streamlined examples to use direct calls without intermediate assignments |
| guides/api-reference.md | Updated API reference examples to match new bang method behavior |
| README.md | Updated main examples to demonstrate new streamlined usage patterns |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| {:ok, response} -> {:ok, Response.text(response)} | ||
| {:error, error} -> {:error, error} | ||
| {:ok, response} -> Response.text(response) | ||
| {:error, error} -> raise error |
There was a problem hiding this comment.
Raising the error directly may not provide a proper exception. Consider using a more specific exception or wrapping in a RuntimeError for better error handling.
| {:error, error} -> raise error | |
| {:error, error} -> raise RuntimeError, message: inspect(error) |
| {:ok, response} -> {:ok, Response.text_stream(response)} | ||
| {:error, error} -> {:error, error} | ||
| {:ok, response} -> Response.text_stream(response) | ||
| {:error, error} -> raise error |
There was a problem hiding this comment.
Raising the error directly may not provide a proper exception. Consider using a more specific exception or wrapping in a RuntimeError for better error handling.
| {:error, error} -> raise error | |
| {:error, error} -> raise RuntimeError, message: to_string(error) |
| {:ok, response} -> Response.object(response) | ||
| {:error, error} -> raise error |
There was a problem hiding this comment.
Raising the error directly may not provide a proper exception. Consider using a more specific exception or wrapping in a RuntimeError for better error handling.
| {:ok, response} -> Response.object_stream(response) | ||
| {:error, error} -> raise error |
There was a problem hiding this comment.
Raising the error directly may not provide a proper exception. Consider using a more specific exception or wrapping in a RuntimeError for better error handling.
Summary
This PR fixes the bang variant methods to follow idiomatic Elixir patterns and streamlines documentation examples throughout the codebase.
Changes Made
Bang Method Behavior Fix
Documentation Streamlining
Files Updated
Testing
Breaking Changes
This is a breaking change for users who were pattern matching on bang method results.
Users should update their code to either: