fix: nil-safe content.strip in chat_completion final-response paths#49
Merged
Conversation
Raix raised NoMethodError when an LLM returned a final assistant message with null content (e.g. Gemini on certain stop conditions). Three call sites in chat_completion.rb did `content.strip` without guarding against nil; now use `content.to_s.strip`.
CI on main has been failing since the 2.0.2 release because the gemspec version bump didn't include a corresponding lockfile update. This unblocks deployment-mode bundle install on CI. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Symptom
A long-running chat completion crashed with the above when the LLM returned a final assistant message containing
"content": null. The same input on retry succeeded (the model returned non-nil content the second time), confirming the bug is non-deterministic and depends on what the provider emits.When it happens
Some providers — notably Gemini under certain stop conditions (length-limit hit, tool-exhaustion fallback, etc.) — return:
{ "choices": [ { "message": { "content": null, "tool_calls": [] } } ] }Raix::ChatCompletioncallscontent.stripon the final response in three places without guarding against nil:lib/raix/chat_completion.rb:175(max_tool_calls exceeded path)lib/raix/chat_completion.rb:218(stop_tool_calls_and_respond!path)lib/raix/chat_completion.rb:226(no tool_calls fall-through path)When
contentisnil,nil.stripraisesNoMethodErrorand the whole completion blows up.Fix
Change
content.strip→content.to_s.stripat each of the three call sites.nil.to_sis"", so a nil response now coerces to an empty string instead of raising. No other behavior changes — non-nil content still strips identically.Tests
Added
spec/raix/nil_content_spec.rbcovering each of the three branches by stubbingruby_llm_requestto return{"choices" => [{"message" => {"content" => nil, ...}}]}:max_tool_callsexceeded with nil content (line 175 path)stop_tool_calls_and_respond!with nil content (line 218 path)Each spec verifies
chat_completionreturns""instead of raisingNoMethodError. Confirmed the specs fail onmainand pass with the fix applied.bundle exec rakeruns clean: 96 examples, 0 failures, 37 files rubocop-clean.