Skip to content

Fix/562 faraday logging memory bloat#661

Merged
crmne merged 3 commits into
crmne:mainfrom
sergiobayona:fix/562-faraday-logging-memory-bloat
Mar 9, 2026
Merged

Fix/562 faraday logging memory bloat#661
crmne merged 3 commits into
crmne:mainfrom
sergiobayona:fix/562-faraday-logging-memory-bloat

Conversation

@sergiobayona

@sergiobayona sergiobayona commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

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: true option which is not recognized by Faraday's logging formatter. The only valid keys are :headers, :bodies, :errors, and :log_level (source).

Type of change

  • [X ] Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Performance improvement

Scope check

  • [ X] I read the Contributing Guide
  • [ 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

  • 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

sergiobayona and others added 3 commits March 9, 2026 10:32
`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.
@codecov

codecov Bot commented Mar 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.50%. Comparing base (8ca693c) to head (78899d7).
⚠️ Report is 1 commits behind head on main.

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.
📢 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
crmne merged commit 65729fd into crmne:main Mar 9, 2026
25 checks passed
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
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.

[BUG] Faraday logging bodies is memory intensive when passing a large PDF

2 participants