Skip to content

fix(oci): support cohere tool-result turns and citation document ids - #46181

Open
aashutosh-yadav wants to merge 1 commit into
BerriAI:mainfrom
aashutosh-yadav:litellm_oci_cohere_tool_result_turns
Open

aashutosh-yadav wants to merge 1 commit into
BerriAI:mainfrom
aashutosh-yadav:litellm_oci_cohere_tool_result_turns

Conversation

@aashutosh-yadav

@aashutosh-yadav aashutosh-yadav commented Oct 13, 2026 •

Copy link
Copy Markdown

TLDR

Problem this solves:

  • OCI rejects a cohere agent-loop turn with 400 when message is set and the last chatHistory entry holds tool results
  • Any cohere response carrying citations crashes while parsing, because OCI writes documentIds but the model reads document_ids

How it solves it:

  • Trailing tool results move to the top-level toolResults field with an empty message, and the user and chatbot turns stay in chatHistory
  • Every message converts 1:1, so no turn is dropped or reordered
  • The citation model binds documentIds as the wire alias for document_ids

User Flow

Before: a developer running an agent loop against cohere.command-a-03-2025 on OCI gets a 400 on the turn after a tool call, and cited answers crash parsing

  1. The agent sends POST /v1/chat/completions with a tool message last
  2. The gateway sets message to the last user text and leaves the tool result in chatHistory
  3. OCI answers 400 with "cannot specify message if the last entry in chat history contains tool results"
  4. When a reply does come back carrying citations, parsing raises on document_ids

After: the same loop continues past the tool call and cited answers parse

  1. The agent sends the same POST /v1/chat/completions with a tool message last
  2. The gateway sends message="", the results in top-level toolResults, and chatHistory ending in the assistant tool call, with the user question kept
  3. OCI returns 200 and the model answers on the tool output
  4. The reply's citations parse from documentIds

Relevant issues

Fixes #46157

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/llms/oci/ -q (424 passed, 5 skipped)
  • My PR passes all required CI/CD checks (the type check, ruff strict, type discipline, and test quality gates all pass against the merge base; production files are ruff-clean)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Screenshots / Proof of Fix

A live call to OCI was not run: the author had no OCI credentials, so no real provider request with cost is included. The evidence instead is the exact payload that would have been signed and sent, dumped offline before and after, plus the regression tests added here, and both halves fail on the old code. A reviewer with an OCI compartment can reproduce the 400 fix by replaying the After payload shape below.

Before (merge base)

  1. Dump the signed chat request for a tool-result turn by monkeypatching OCIChatConfig.sign_request to print request_data["chatRequest"]
  2. The dump shows message set to the user question and chatHistory = [CHATBOT, TOOL], the shape OCI answers 400 to
  3. Any response with citations raises on document_ids

After (1e5807b)

  1. The same dump shows message = "", results in top-level toolResults, and chatHistory = [USER, CHATBOT]
  2. Regression tests for the tool-result turn, the parallel tool-result turn, and the citation alias pass; reverting either fix fails its tests

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Live OCI verification is missing, no credentials on the author's machine; the request shape is shown offline and covered by regression tests

Low

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@codspeed

codspeed Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 31 untouched benchmarks


Comparing aashutosh-yadav:litellm_oci_cohere_tool_result_turns (1e5807b) with main (cb94567)

Open in CodSpeed

@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

OCI rejects a cohere chat request that sets `message` while the last
chatHistory entry carries tool results with a 400 ("cannot specify
message if the last entry in chat history contains tool results"). Lift
trailing tool-result messages into top-level `toolResults` with an empty
`message`, keep the user and chatbot turns in `chatHistory`, and convert
every message 1:1 instead of reordering or dropping turns.

Also fix a crash parsing cohere citations: OCI's SDK exposes the field
as `documentIds` while the model read `document_ids`, so any citation
broke attribute access. Bind the alias explicitly.

Regression tests cover both directions: the request payload shape for a
tool-result turn, and the citation document ids alias.

This branch has not been deployed

No deployments
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]: OCI provider: Cohere models fail on the call after a tool call (request shape, and citations use documentIds)

1 participant