openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

fix(inference): use normalized role for Responses content kind (#3624)

Open oxoxDev opened this issue 5 months ago • 2 comments

Summary

  • Responses-API requests that contained a tool result failed with OpenAI 400 Invalid value: 'input_text'; the tool turn was emitted as an assistant item carrying input_text.
  • Key the content-part kind on the normalized role (not the raw message.role) so role and kind can never diverge.
  • Fix the multi-turn test that codified the rejected shape, and add a regression asserting no assistant-role item ever carries input_text.

Problem

build_responses_prompt (src/openhuman/inference/provider/compatible_parse.rs) sets the item role via normalize_responses_role, which folds tool (and any non-user role) into assistant. But the content-part kind was chosen from the raw message.role — so a tool message became role assistant while keeping input_text. The Responses API only accepts output_text/refusal for assistant items, returning:

Invalid value: 'input_text'. Supported values are: 'output_text' and 'refusal'. (param: input[N].content[0])

Any conversation turn with a tool result hit this. Live on the current release; 2332 events / 17 users (Sentry TAURI-RUST-8FQ). This is the real-OpenAI {"error":{…}} shape, distinct from the Codex {"detail":…} shape handled in #3288.

Solution

  • Compute let role = normalize_responses_role(&message.role) once and use it for both the item role and the kind branch (if role == "assistant" { output_text } else { input_text }). Single source of truth — a future role remap cannot reintroduce the skew.
  • compatible_tests.rs: the existing build_responses_prompt_preserves_multi_turn_history asserted the tool item as input_text (the exact rejected shape) — corrected to output_text.
  • Added build_responses_prompt_tool_role_uses_output_text asserting the invariant directly.

Submission Checklist

If a section does not apply to this change, mark the item as N/A with a one-line reason. Do not delete items.

  • [x] Tests added or updated (happy path + at least one failure / edge case) per Testing Strategy
  • [x] Diff coverage ≥ 80% — both changed logic branches are exercised by the two unit tests
  • [x] N/A: behaviour-only bug fix to existing request serialization, no feature row in the coverage matrix
  • [x] N/A: no coverage-matrix row affected, so no feature IDs to list
  • [x] No new external network dependencies introduced
  • [x] N/A: pure request-serialization fix, touches no release-cut surface
  • [x] Linked issue closed via Closes #3624 in the ## Related section

Impact

  • Desktop (Rust core inference). Restores OpenAI Responses-API calls for any turn carrying a tool result; eliminates the TAURI-RUST-8FQ 400 flood.
  • No API/schema change, no migration. Behavior change is strictly: tool turns now serialize as output_text.

Related

  • Closes: #3624
  • Sentry-Issue: TAURI-RUST-8FQ
  • Follow-up PR(s)/TODOs: none

AI Authored PR Metadata (required for Codex/Linear PRs)

Linear Issue

  • Key: N/A
  • URL: N/A

Commit & Branch

  • Branch: fix/3624-responses-tool-output-text
  • Commit SHA: 6e020e220971f2053a8f50651ea30f2802b43f66

Validation Run

  • [x] N/A: no frontend change — pnpm --filter openhuman-app format:check
  • [x] N/A: no frontend change — pnpm typecheck
  • [x] Focused tests: cargo test --lib openhuman::inference::provider::compatible::tests::build_responses_prompt (2 passed)
  • [x] Rust fmt/check: cargo fmt --check clean on touched files; lib compiled clean
  • [x] N/A: no app/src-tauri change — Tauri fmt/check

Validation Blocked

  • command: N/A
  • error: N/A
  • impact: N/A

Behavior Changes

  • Intended behavior change: tool-role messages serialize as output_text content in Responses-API requests.
  • User-visible effect: OpenAI Responses inference no longer fails after a tool call.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed a compatibility issue with OpenAI-compatible inference providers where tool responses were incorrectly formatted, causing validation errors. The system now properly normalizes role assignments and uses the correct content format for API requests.
  • Tests

    • Added regression test to verify proper handling of tool-to-assistant role conversion in API payloads.

oxoxDev avatar Jun 12 '26 13:06 oxoxDev

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 91ed96ba-92c5-437b-a938-500998efb7f3

📥 Commits

Reviewing files that changed from the base of the PR and between 6e020e220971f2053a8f50651ea30f2802b43f66 and ba90192f7b76ad98a7ed19607f8bc9d54ecd0323.

📒 Files selected for processing (1)
  • src/openhuman/voice/always_on.rs
✅ Files skipped from review due to trivial changes (1)
  • src/openhuman/voice/always_on.rs

📝 Walkthrough

Walkthrough

build_responses_prompt now derives content-part kind from the normalized role (via normalize_responses_role) so tool messages folded to assistant use output_text; tests updated and a regression test added. A small non-functional reformat in always-on capture was also applied.

Changes

Responses API Role/Kind Consistency Fix

Layer / File(s) Summary
Normalize role before determining content kind
src/openhuman/inference/provider/compatible_parse.rs
build_responses_prompt computes role = normalize_responses_role(&message.role) and uses that normalized role both for ResponsesInput.role and to pick ResponsesContentPart.kind so tool→assistant normalization produces output_text instead of input_text.
Test updates and regression test
src/openhuman/inference/provider/compatible_tests.rs
Updated build_responses_prompt_preserves_multi_turn_history to expect tool content as output_text, and added build_responses_prompt_tool_role_uses_output_text to assert that every assistant-role item has content[0].kind == "output_text".
Always-on capture reformat (non-functional)
src/openhuman/voice/always_on.rs
Reformatted device_name initialization in capture_on_thread to a single-line expression; no semantic change.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • tinyhumansai/openhuman#3619: Overlaps in src/openhuman/voice/always_on.rs area; that PR adds diagnostic changes around always-on capture device/host info.

Suggested labels

rust-core

Poem

🐰 I hopped through roles both sharp and light,
A tool now dresses in assistant's white,
No more input_text to trip the call,
Tests stand guard — they catch the fall. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ❓ Inconclusive The PR contains a minor formatting change (style(voice)) to always_on.rs that is out of scope for the functional fix. However, the author acknowledges this is only to satisfy CI checks and offers to drop it. Consider removing the formatting commit (always_on.rs) unless it is required by project CI; it is orthogonal to the functional fix for #3624 and introduces unnecessary scope.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately describes the main functional change: fixing Responses API serialization by ensuring the content kind is derived from the normalized role, which directly addresses the root cause in #3624.
Linked Issues check ✅ Passed The PR successfully addresses all coding requirements from #3624: it derives content-kind from normalized role, updates test assertions to expect output_text for assistant turns, and adds a regression test asserting assistant-role items never carry input_text.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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

coderabbitai[bot] avatar Jun 12 '26 13:06 coderabbitai[bot]

Heads-up: the second commit (style(voice): apply cargo fmt to always_on.rs) is not part of the fix. always_on.rs:677 had pre-existing rustfmt drift on main (v0.57.39) that was failing cargo fmt --all --check in Rust Quality + Frontend Quality for every PR. It's the exact one-line output of cargo fmt, no logic change — included only to get this PR's CI green. Happy to drop it if main is fixed separately.

oxoxDev avatar Jun 12 '26 13:06 oxoxDev