Skip to content

fix(translation): drop empty assistant messages in OpenAI Chat encoding - #379

Open
Pana wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
Pana:fix/openai-chat-empty-assistant-message
Open

fix(translation): drop empty assistant messages in OpenAI Chat encoding#379
Pana wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
Pana:fix/openai-chat-empty-assistant-message

Conversation

@Pana

@Pana Pana commented Aug 12, 2026

Copy link
Copy Markdown

What

Use this project with moonshot kimi-k3 model, will encounter error: Invalid request: the message at position 5 with role 'assistant' must not be empty

Why

So this PR is used to improve compatibility to kimi API

Closes #

How tested

  • uv run ruff check . clean
  • uv run mypy switchyard clean
  • uv run pytest tests/ green
  • Manual smoke (describe what was run)

Checklist

  • One class per file; filename = snake_case of the primary class.
  • New public symbols exported from switchyard/__init__.py.__all__ if intended for downstream use.
  • Unit tests added for new components / bug fixes.
  • README / --help updated if customer-facing surface changed.
  • Commits signed off (Signed-off-by: Your Name <email>) per the DCO.

Notes for reviewers

Anything reviewers should pay extra attention to — risky paths, follow-up tickets, intentional trade-offs.

Summary by CodeRabbit

  • Bug Fixes
    • Improved OpenAI Chat request translation by omitting empty assistant messages that contain no tool calls.
    • Preserved assistant messages that contain tool calls, even when their text content is null.
    • Corrected handling of assistant turns containing only private thinking content while retaining surrounding user messages.

@Pana
Pana requested a review from a team as a code owner August 12, 2026 07:15
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 32785209-1074-4cdf-b9c8-1f1673eacf82

📥 Commits

Reviewing files that changed from the base of the PR and between 2bef154 and b513d82.

📒 Files selected for processing (2)
  • crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
  • crates/switchyard-translation/tests/request_translation.rs

Walkthrough

The OpenAI Chat encoder now removes assistant messages with empty content when they have no tool calls. A regression test covers Anthropic assistant turns that contain only private thinking blocks.

Changes

OpenAI Chat message filtering

Layer / File(s) Summary
Empty assistant message filtering
crates/switchyard-translation/src/codecs/openai_chat/buffered.rs
The encoder removes assistant messages with empty-string content when they have no tool calls. Tool-call messages remain preserved.
Thinking-only turn regression coverage
crates/switchyard-translation/tests/request_translation.rs
The test verifies that thinking-only Anthropic assistant turns are omitted and surrounding user messages remain intact.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

I’m a rabbit with a tidy tray,
Empty assistant turns hop away.
Tool calls stay, both crisp and bright,
Thinking-only turns fade from sight.
Tests keep the message path just right.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: omitting empty assistant messages during OpenAI Chat encoding.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@Pana
Pana force-pushed the fix/openai-chat-empty-assistant-message branch from b513d82 to 2eae3ea Compare August 12, 2026 07:36
@ayushag-nv

Copy link
Copy Markdown
Contributor

@Pana Thanks for adding the fix. Can you please add the reproduction steps for the issue, and before the fix snapshot with error and after the fix snapshot.

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