Skip to content

fix(oci): resolve Cohere multi-turn tool calling and citation parsing (#46157) - #46159

Open
pheonix14 wants to merge 1 commit into
BerriAI:mainfrom
pheonix14:fix/oci-cohere-multi-turn-tools
Open

pheonix14 wants to merge 1 commit into
BerriAI:mainfrom
pheonix14:fix/oci-cohere-multi-turn-tools

Conversation

@pheonix14

Copy link
Copy Markdown

TLDR

Problem this solves:

  • Multi-turn tool calling on OCI Cohere models fails on the second turn with 400 invalid request: cannot specify message if the last entry in chat history contains tool results.
  • Responses containing citations fail response validation because OCI returns camelCase documentIds while CohereCitation required document_ids.

How it solves it:

  • In OCIChatConfig.transform_request, when the conversation ends with tool messages, trailing CohereToolMessage entries are popped from chatHistory into top-level toolResults, and top-level message is set to "".
  • Added toolResults field to CohereChatRequest.
  • In CohereCitation, added model_config = ConfigDict(populate_by_name=True) and declared document_ids with alias documentIds and default [].

User Flow

Before: an application executing multi-turn tool calling with an OCI Cohere model crashes on the second turn with HTTP 400 or response validation failure

  1. The client sends turn 1: user prompt requesting an action with tool definitions.
  2. The model returns a tool call.
  3. The client executes the tool and sends turn 2 with the assistant message and role: "tool" result.
  4. OCI returns 400 invalid request: cannot specify message if the last entry in chat history contains tool results.
  5. If citations are present, response parsing fails with Response cannot be casted to CohereChatResult: Field required: chatResponse.citations.0.document_ids.

After: the second turn succeeds and returns the final assistant answer

  1. The client sends turn 1 with user prompt and tool definitions.
  2. The model returns a tool call.
  3. The client sends turn 2 with the assistant message and role: "tool" result.
  4. transform_request maps trailing tool outputs to toolResults and sets message="", which OCI accepts.
  5. CohereCitation parses camelCase documentIds from OCI seamlessly and returns the completed ModelResponse.

Relevant issues

Fixes #46157

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks
  • 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

Type

🐛 Bug Fix

…BerriAI#46157)

- Pop trailing CohereToolMessage entries from chatHistory into top-level toolResults and clear message on tool-result turns, preventing OCI 400 'cannot specify message if last entry in chat history contains tool results'
- Add toolResults field to CohereChatRequest
- Update CohereCitation to accept camelCase documentIds alias as returned by OCI
- Add comprehensive unit tests covering single and parallel multi-turn tool responses and citation validation
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codspeed

codspeed Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing pheonix14:fix/oci-cohere-multi-turn-tools (d836041) with main (0eff31a)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (9621a60) during the generation of this report, so 0eff31a was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@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!

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)

2 participants