Skip to content

[BUG] NoMethodError: undefined method 'empty?' for nil in OpenRouter parse_completion_response #744

@edomaia-dentero

Description

@edomaia-dentero

Basic checks

  • I searched existing issues - this hasn't been reported
  • I can reproduce this consistently
  • This is a RubyLLM bug, not my application code

What's broken?

RubyLLM::Providers::OpenRouter::Chat#parse_completion_response calls .empty? on data without guarding for nil. When OpenRouter returns a response where data is absent (nil), this raises a NoMethodError and the entire request fails.

How to reproduce

  1. Configure RubyLLM with the OpenRouter provider (ruby_llm 1.14.1).
  2. Create a chat with tools/schema enabled (tool-calling flow).
  3. Call chat.ask(...).
  4. Intermittently, OpenRouter returns a response where parsed data is nil.
  5. RubyLLM crashes in RubyLLM::Providers::OpenRouter::Chat#parse_completion_response when it calls data.empty?.

Note: this is intermittent in production; we cannot reproduce deterministically on every request.

Expected behavior

RubyLLM should handle missing or nil response data safely in OpenRouter parsing (e.g. nil guard before .empty?) and return a controlled error/result instead of raising NoMethodError.

What actually happened

We get this runtime error from ruby_llm:

NoMethodError: undefined method 'empty?' for nil

# lib/ruby_llm/providers/openrouter/chat.rb:55
return if data.empty?

Backtrace top:

RubyLLM::Providers::OpenRouter::Chat#parse_completion_response (lib/ruby_llm/providers/openrouter/chat.rb:55)
RubyLLM::Provider#sync_response (lib/ruby_llm/provider.rb:262)
RubyLLM::Provider#complete (lib/ruby_llm/provider.rb:62)
RubyLLM::Chat#complete / #handle_tool_calls (lib/ruby_llm/chat.rb)
This happens in production background jobs (Rails app) calling RubyLLM.chat(...).ask(...) through OpenRouter.

Environment

  • Ruby version: 3.4.6
  • RubyLLM version: 1.14.1
  • Provider (OpenAI, Google, etc.): OpenRouter
  • OS: Linux (production), macOS locally
  • Framework: Rails 8.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions