Skip to content

Fix bang methods to return naked values and streamline documentation#9

Merged
mikehostetler merged 2 commits into
mainfrom
bug/api-return-types
Sep 15, 2025
Merged

Fix bang methods to return naked values and streamline documentation#9
mikehostetler merged 2 commits into
mainfrom
bug/api-return-types

Conversation

@mikehostetler

Copy link
Copy Markdown
Contributor

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

  • Updated all bang variants (generate_text!, stream_text!, generate_object!, stream_object!) to return naked values instead of {:ok, value} tuples
  • Bang methods now raise on error instead of returning {:error, term} tuples
  • Fixed type specs to use "no_return()" instead of tuple returns
  • All bang methods now follow standard Elixir conventions

Documentation Streamlining

  • Removed unnecessary intermediate variable assignments in examples
  • Changed from verbose pattern with intermediate variables to streamlined direct calls
  • Updated examples across README, guides, and module documentation

Files Updated

  • lib/req_llm.ex - Main API documentation
  • lib/req_llm/generation.ex - Generation module documentation
  • README.md - Main project examples
  • guides/getting-started.md - Getting started guide
  • guides/api-reference.md - API reference examples
  • usage-rules.md - Usage guidelines

Testing

  • All files compile successfully
  • Type specs are correct
  • Documentation examples follow consistent patterns

Breaking Changes

This is a breaking change for users who were pattern matching on bang method results.

Users should update their code to either:

  1. Use the non-bang variants if they need tuple returns
  2. Use direct assignment with bang methods
  3. Handle exceptions if needed

mikehostetler and others added 2 commits September 15, 2025 07:06
- 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>
@coveralls

coveralls commented Sep 15, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 20c5d904b413fefeff5b27db94af2ff51526bb73-PR-9

Details

  • 0 of 29 (0.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.02%) to 21.938%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/req_llm/generation.ex 0 8 0.0%
lib/mix/tasks/stream_text.ex 0 21 0.0%
Totals Coverage Status
Change from base Build fad21d4ab875c379a3ec366e984610fde1d72512: -0.02%
Covered Lines: 498
Relevant Lines: 2270

💛 - Coveralls

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread lib/req_llm/generation.ex
{:ok, response} -> {:ok, Response.text(response)}
{:error, error} -> {:error, error}
{:ok, response} -> Response.text(response)
{:error, error} -> raise error

Copilot AI Sep 15, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
{:error, error} -> raise error
{:error, error} -> raise RuntimeError, message: inspect(error)

Copilot uses AI. Check for mistakes.
Comment thread lib/req_llm/generation.ex
{:ok, response} -> {:ok, Response.text_stream(response)}
{:error, error} -> {:error, error}
{:ok, response} -> Response.text_stream(response)
{:error, error} -> raise error

Copilot AI Sep 15, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
{:error, error} -> raise error
{:error, error} -> raise RuntimeError, message: to_string(error)

Copilot uses AI. Check for mistakes.
Comment thread lib/req_llm/generation.ex
Comment on lines +458 to +459
{:ok, response} -> Response.object(response)
{:error, error} -> raise error

Copilot AI Sep 15, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread lib/req_llm/generation.ex
Comment on lines +488 to +489
{:ok, response} -> Response.object_stream(response)
{:error, error} -> raise error

Copilot AI Sep 15, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@mikehostetler mikehostetler merged commit d29c812 into main Sep 15, 2025
2 checks passed
@mikehostetler mikehostetler deleted the bug/api-return-types branch September 15, 2025 14:18
@mikehostetler mikehostetler restored the bug/api-return-types branch September 26, 2025 19:11
@mikehostetler mikehostetler deleted the bug/api-return-types branch September 26, 2025 19:41
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

Successfully merging this pull request may close these issues.

3 participants