Fix/562 faraday logging memory bloat#661
Merged
crmne merged 3 commits intoMar 9, 2026
Merged
Conversation
`setup_logging` hardcoded `bodies: true`, causing Faraday's formatter to call `dump_body` → `pretty_inspect` on every request/response body, even when the logger level was above DEBUG. For large payloads (e.g. base64-encoded PDFs), this allocated 66 MB+ of memory unnecessarily. Fix: set `bodies:` to `RubyLLM.logger.debug?` so body logging is only enabled when the logger is actually at DEBUG level.
Faraday's logging formatter (v2.14.1) only recognizes `:headers`, `:bodies`, `:errors`, and `:log_level`. Ref: https://github.com/lostisland/faraday/blob/v2.14.1/lib/faraday/logging/formatter.rb#L11
crmne
approved these changes
Mar 9, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #661 +/- ##
=======================================
Coverage 81.50% 81.50%
=======================================
Files 116 116
Lines 5483 5483
Branches 1432 1432
=======================================
Hits 4469 4469
Misses 1014 1014 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
14 tasks
crmne
pushed a commit
that referenced
this pull request
Mar 9, 2026
## What this does Commit 1: Removes response: false from Connection.basic — the same unrecognized Faraday logger option removed from setup_logging in #661. Faraday's logging formatter (v2.14.1) only recognizes :headers, :bodies, :errors, and :log_level ([source](https://github.com/lostisland/faraday/blob/v2.14.1/lib/faraday/logging/formatter.rb#L11)). Commit 2: Converts the remaining eager logger.debug call in OpenRouter::Images to lazy block form. This is the same pattern fixed across other providers in 90b8dd1 — the identical call in gemini/images.rb was converted but this one was missed. ## Type of change - [ X] Bug fix - [ ] New feature - [ ] Breaking change - [ ] Documentation - [ ] Performance improvement ## Scope check - [ X] I read the [Contributing Guide](https://github.com/crmne/ruby_llm/blob/main/CONTRIBUTING.md) - [ X] This aligns with RubyLLM's focus on **LLM communication** - [ X] This isn't application-specific logic that belongs in user code - [ X] This benefits most users, not just my specific use case ## Required for new features <!-- Skip this section for bug fixes and documentation --> - [ ] I opened an issue **before** writing code and received maintainer approval - [ ] Linked issue: #___ **PRs for new features or enhancements without a prior approved issue will be closed.** ## Quality check - [ X] I ran `overcommit --install` and all hooks pass - [ X] I tested my changes thoroughly - [ ] For provider changes: Re-recorded VCR cassettes with `bundle exec rake vcr:record[provider_name]` - [ X] All tests pass: `bundle exec rspec` - [ ] I updated documentation if needed - [ ] I didn't modify auto-generated files manually (`models.json`, `aliases.json`) ## AI-generated code - [ ] I used AI tools to help write this code - [ ] I have reviewed and understand all generated code (required if above is checked) ## API changes - [ ] Breaking change - [ ] New public methods/classes - [ ] Changed method signatures - [ X] No API changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
Fixes #562 — Faraday body logging causes unnecessary memory allocation when sending large payloads (e.g. base64-encoded PDFs), even when the log level is set to INFO or higher.
Root cause: setup_logging in Connection hardcoded bodies: true on the Faraday logger middleware. This caused Faraday's formatter to always call dump_body → pretty_inspect (from Ruby's pp gem), serializing the entire request/response body into a string for logging — regardless of whether that log line would ever be emitted.
Fix: Replace bodies: true with bodies: RubyLLM.logger.debug? so body serialization only happens when the logger is actually at DEBUG level. At INFO or higher (the default), log_body? returns false and dump_body is never called.
Also removes the
response: trueoption which is not recognized by Faraday's logging formatter. The only valid keys are :headers, :bodies, :errors, and :log_level (source).Type of change
Scope check
Required for new features
PRs for new features or enhancements without a prior approved issue will be closed.
Quality check
overcommit --installand all hooks passbundle exec rake vcr:record[provider_name]bundle exec rspecmodels.json,aliases.json)AI-generated code
API changes