Skip to content

Conversation

finbarr
Copy link
Contributor

@finbarr finbarr commented Jul 23, 2025

Summary

This PR fixes a type mismatch error that occurs when handling errors during streaming responses. The streaming error handlers were passing plain hashes to ErrorMiddleware.parse_error, but parse_error expected Faraday response objects with a body method.

Problem

When errors occurred during streaming, the application would crash with:

NoMethodError: undefined method 'body' for an instance of Hash

Solution

  • Created an ErrorResponse struct that mimics the Faraday::Response interface
  • Updated all streaming error handlers (handle_error_chunk, handle_failed_response, handle_error_event) to use a new create_error_response helper method
  • Added nil-safe handling for the env parameter in streaming contexts
  • Added comprehensive tests to verify the fix

Testing

Added tests to spec/ruby_llm/error_handling_spec.rb that verify:

  • Error chunks are handled correctly with both nil and populated env parameters
  • Failed responses are processed properly
  • The ErrorResponse struct provides the expected interface

All tests pass and the streaming error handling now works consistently with regular response error handling.

🤖 Generated with Claude Code

The streaming error handlers were passing plain hashes to ErrorMiddleware.parse_error,
but parse_error expected Faraday response objects with a body method. This caused
NoMethodError when errors occurred during streaming.

Changes:
- Created ErrorResponse struct to mimic Faraday::Response interface
- Updated streaming error handlers to use create_error_response helper
- Added nil-safe handling for env parameter in streaming contexts
- Added comprehensive tests to existing error_handling_spec.rb

This ensures streaming errors are handled consistently with regular response errors.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link

codecov bot commented Jul 30, 2025

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.00%. Comparing base (a9a1446) to head (34657a1).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/ruby_llm/streaming.rb 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #292      +/-   ##
==========================================
+ Coverage   86.64%   87.00%   +0.36%     
==========================================
  Files          79       79              
  Lines        3129     3133       +4     
  Branches      613      614       +1     
==========================================
+ Hits         2711     2726      +15     
+ Misses        418      407      -11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@crmne
Copy link
Owner

crmne commented Jul 30, 2025

Thanks for your work on this! We ended up going with #273 which has been merged. The streaming error handling issue is now resolved.

@crmne crmne closed this Jul 30, 2025
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.

2 participants