Skip to content

fix(oci): make Cohere {{trace}} judges work (re-land #30646, lint-clean) - #30780

Open
fede-kamel wants to merge 1 commit into
BerriAI:mainfrom
fede-kamel:litellm_oci_cohere_judge_reland
Open

fede-kamel wants to merge 1 commit into
BerriAI:mainfrom
fede-kamel:litellm_oci_cohere_judge_reland

Conversation

@fede-kamel

@fede-kamel fede-kamel commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • MLflow {{trace}} judges fail against OCI Cohere models
  • Tools with array-typed parameters draw an OCI 500
  • The tool-result continuation turn draws an OCI 400
  • Tool-grounded responses with citations fail to parse

How it solves it:

  • Maps JSON-schema collection types to lowercase list / dict
  • Sends continuations as empty message plus top-level toolResults
  • Keeps the user turn in chatHistory, drops TOOL entries
  • Makes citation document_ids and search-query generation_id optional

User Flow

Before: a developer running an MLflow {{trace}} judge against an OCI Cohere model through the gateway sees every judge run fail

  1. They send POST http://localhost:4000/v1/chat/completions for cohere-judge with a get_span tool whose attributes_to_fetch parameter is a JSON-schema array
  2. The response is HTTP 500 wrapping OCI "Internal Server Error", so the judge never gets its first tool call
  3. They retry the continuation turn, the same POST with the assistant tool call and tool result appended, and get HTTP 500 wrapping OCI 400 "cannot specify message if the last entry in chat history contains tool results"
  4. The judge aborts and the trace gets no score

After: the same requests complete and the judge scores the trace

  1. They send the same POST http://localhost:4000/v1/chat/completions with the array-typed tool parameter
  2. HTTP 200 comes back with a get_span entry in tool_calls
  3. They append the assistant tool call and tool result and POST again
  4. HTTP 200 comes back with the grounded answer ("There are 47 spans in the trace.") and real token usage

Relevant issues

Re-lands #30646, which was rolled back in a batch stabilization revert (commit 15a7c687) along with several unrelated PRs. The greptile-review hardening applied on top of the original merge is folded in. A user in this PR's thread is currently blocked on this fix for their OCI GenAI deployment and independently verified the top-level toolResults request shape against the raw OCI API

The three underlying bugs, all hit by judges that send tools and run an agentic tool-calling loop:

  1. Tool parameter types: a JSON-schema array parameter mapped to the bare string "List", which OCI's Cohere backend rejects with a 500. The mapping now emits the lowercase builtins list / dict
  2. Continuation turns: litellm pulled the last user message into the top-level message and emitted the tool result as a TOOL entry in chatHistory, which OCI rejects. OCI carries the current turn's results in a dedicated top-level toolResults field, so the transform now sends an empty message, keeps the user turn in chatHistory, and puts results in toolResults, matching the langchain-oracle reference
  3. Response parsing: tool-grounded answers return citations carrying documentIds and no document_ids, which sank the whole parse because CohereCitation.document_ids was required. That field and CohereSearchQuery.generation_id are now optional

Latest push rebases onto the current litellm_internal_staging tip (d5b91b94d3) and renames one cross-module helper to stay under the basedpyright private-usage budget, with no semantic changes. Verified locally at the new tip: ruff format --check clean, all three budget gates pass, the OCI unit suite passes (372 tests), and the four gated live Cohere tool integration tests pass against real OCI GenAI

Linear ticket

Pre-Submission checklist

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

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Live proxy against real OCI GenAI in us-chicago-1, credentials supplied via the OCI_* environment variables so no secrets appear in requests. Config:

model_list:
  - model_name: cohere-judge
    litellm_params:
      model: oci/cohere.command-a-03-2025

Payloads, identical on both sides:

cat > case1_array_param.json <<'EOF'
{
  "model": "cohere-judge",
  "messages": [{"role": "user", "content": "How many spans are in the trace? Use the tool."}],
  "tools": [{"type": "function", "function": {"name": "get_span", "description": "Fetch a span.", "parameters": {"type": "object", "properties": {"attributes_to_fetch": {"type": "array", "items": {"type": "string"}, "description": "attrs"}}, "required": []}}}],
  "max_tokens": 120
}
EOF
cat > case2_continuation.json <<'EOF'
{
  "model": "cohere-judge",
  "messages": [
    {"role": "user", "content": "How many spans are in the trace?"},
    {"role": "assistant", "content": "I will look at the root span.", "tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "get_span", "arguments": "{}"}}]},
    {"role": "tool", "tool_call_id": "call_1", "content": "{\"span_count\": 47}"}
  ],
  "tools": [{"type": "function", "function": {"name": "get_span", "description": "Fetch a span.", "parameters": {"type": "object", "properties": {"attributes_to_fetch": {"type": "array", "items": {"type": "string"}, "description": "attrs"}}, "required": []}}}],
  "max_tokens": 120
}
EOF

Before (d5b91b9)

Tool with an array-typed parameter

  1. curl -s -w "\nHTTP_STATUS:%{http_code}\n" http://localhost:4000/v1/chat/completions -H "Content-Type: application/json" -d @case1_array_param.json
  2. Observed output:
{"error":{"message":"litellm.APIConnectionError: OciException - { \"code\": \"500\", \"message\": \"Internal Server Error.\" }. Received Model Group=cohere-judge\nAvailable Model Group Fallbacks=None","type":null,"param":null,"code":"500"}}
HTTP_STATUS:500

Tool-result continuation

  1. curl -s -w "\nHTTP_STATUS:%{http_code}\n" http://localhost:4000/v1/chat/completions -H "Content-Type: application/json" -d @case2_continuation.json
  2. Observed output:
{"error":{"message":"litellm.APIConnectionError: OciException - { \"code\": \"400\", \"message\": \"{\\\"id\\\":\\\"/48465B6E3414EB5CEF9FBF24621B9CE5/4D91F9296C2336B8AD0BE45678E34977\\\",\\\"message\\\":\\\"invalid request: cannot specify message if the last entry in chat history contains tool results\\\"}\" }. Received Model Group=cohere-judge\nAvailable Model Group Fallbacks=None","type":null,"param":null,"code":"500"}}
HTTP_STATUS:500

After (2d4989f)

Tool with an array-typed parameter

  1. curl -s -w "\nHTTP_STATUS:%{http_code}\n" http://localhost:4000/v1/chat/completions -H "Content-Type: application/json" -d @case1_array_param.json
  2. Observed output:
{"id":"chatcmpl-0bbf5260-da75-4971-851e-d36e26861cf5","created":1786994440,"model":"cohere-judge","object":"chat.completion","choices":[{"finish_reason":"stop","index":0,"message":{"content":"I will use the get_span tool to find out how many spans are in the trace.","role":"assistant","tool_calls":[{"function":{"arguments":"{}","name":"get_span"},"id":"call_8a39c5fb17cbc97d37f08b4c","type":"function"}]}}],"usage":{"completion_tokens":23,"prompt_tokens":21,"total_tokens":44}}
HTTP_STATUS:200

Tool-result continuation

  1. curl -s -w "\nHTTP_STATUS:%{http_code}\n" http://localhost:4000/v1/chat/completions -H "Content-Type: application/json" -d @case2_continuation.json
  2. Observed output:
{"id":"chatcmpl-2678a086-7e6e-4d69-bb32-c1443534728d","created":1786994442,"model":"cohere-judge","object":"chat.completion","choices":[{"finish_reason":"stop","index":0,"message":{"content":"There are 47 spans in the trace.","role":"assistant"}}],"usage":{"completion_tokens":10,"prompt_tokens":36,"total_tokens":46}}
HTTP_STATUS:200

Type

Bug Fix

Caveats (if any)

  • OCI Cohere still rejects tool_choice, a pre-existing limitation, omit the param
  • Live integration tests are gated and skip without OCI credentials

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

@codecov

codecov Bot commented Jun 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/llms/oci/chat/cohere.py 95.00% 1 Missing ⚠️
litellm/llms/oci/chat/transformation.py 80.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@fede-kamel

Copy link
Copy Markdown
Contributor Author

@Sameerlite @mateo-berri this re-lands #30646, which got swept up in the batch stabilization revert (commit 15a7c68) rather than for anything specific to it. I folded in the greptile-review empty-message guard that had been applied on top of the original merge.

It is lint-clean against the current litellm_oss_staging: ruff check and black --check clean, and both budget gates pass locally (scripts/ruff_strict_gate.py and scripts/type_discipline_gate.py exit 0 — the new tool-call helpers use builtin list/dict generics and concrete ChatCompletionAssistantToolCall typing). OCI unit suite is 372 passing, and I re-verified the fix live against us-chicago-1 (single and multi-round tool loops on cohere.command-a-03-2025 and command-latest).

Would appreciate a re-review when you have a moment

@greptile-apps

greptile-apps Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR re-lands the OCI Cohere tool-calling fixes on the current staging tip and removes the dead tool-history representation identified previously.

  • Maps JSON-schema collection parameter types to OCI-compatible lowercase names.
  • Sends continuation results through top-level toolResults with an empty message.
  • Allows response parsing when citation and search metadata fields are absent.
  • Adds unit and gated integration coverage for the corrected OCI request and response shapes.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
litellm/llms/oci/chat/cohere.py Builds USER/CHATBOT history separately from current-turn tool results and removes use of the obsolete tool-history model.
litellm/llms/oci/chat/transformation.py Emits OCI Cohere continuation requests with top-level tool results and an empty message.
litellm/llms/oci/common_utils.py Maps JSON-schema arrays and objects to OCI-compatible lowercase collection type names.
litellm/types/llms/oci.py Adds top-level tool results, relaxes optional response metadata, and removes the previously reported dead CohereToolMessage type.
tests/test_litellm/llms/oci/chat/test_oci_cohere_tool_calls.py Verifies the corrected first-turn and tool-result continuation request shapes.
tests/test_litellm/llms/oci/test_oci_coverage_boost.py Covers tool-result extraction and tool-grounded response parsing.

Reviews (5): Last reviewed commit: "fix(oci): make Cohere {{trace}} judges w..." | Re-trigger Greptile

@fede-kamel
fede-kamel force-pushed the litellm_oci_cohere_judge_reland branch from b2a7502 to 9959751 Compare June 18, 2026 21:09
@fede-kamel

Copy link
Copy Markdown
Contributor Author

Addressed the Greptile finding: removed the now-unreachable CohereToolMessage class and the stale SerializeAsAny machinery on CohereChatRequest.chatHistory (the only thing placed in history now is the base CohereMessage, whose toolCalls field already serializes correctly, so the SerializeAsAny wrapper and its import are no longer load-bearing). No runtime behavior change; verified the serialized chatHistory still preserves toolCalls on the CHATBOT entry. Gates still pass locally (ruff, black, ruff_strict_gate, type_discipline_gate) and the OCI unit suite is 372 passing.

@fede-kamel

Copy link
Copy Markdown
Contributor Author

@greptileai please re-review; the latest commit removes the dead CohereToolMessage class and the stale SerializeAsAny machinery you flagged.

@fede-kamel

Copy link
Copy Markdown
Contributor Author

@Sameerlite @mateo-berri — this re-lands #30646 (rolled back in the batch revert 15a7c687) with the greptile-review hardening already folded in, so there's nothing left to apply on top. CI is fully green now — all 48 checks passing. Should be good to merge whenever you have a moment. Thanks!

@Sameerlite
Sameerlite force-pushed the litellm_oss_staging branch 2 times, most recently from 9faf989 to 6437b81 Compare June 22, 2026 11:57
@Sameerlite
Sameerlite changed the base branch from litellm_oss_staging to litellm_internal_staging June 23, 2026 13:22
@Sameerlite

Copy link
Copy Markdown
Contributor

@fede-kamel Can you rebase this to litellm_internal_staging correctly? I can many unintentional changes

@Sameerlite

Copy link
Copy Markdown
Contributor

Thanks for the contribution! Greptile scored 5/5 and CI is green — the code itself looks solid. The remaining issue is that the diff includes ~80 unintentional files (README, Bedrock, vertex_ai, proxy_server, etc.) beyond the OCI scope. Could you do a clean rebase onto litellm_internal_staging to get the diff down to just the OCI Cohere judge fix? Once the diff is clean, this should be ready!

@fede-kamel
fede-kamel force-pushed the litellm_oci_cohere_judge_reland branch from 9959751 to 50169f3 Compare June 24, 2026 04:46
@fede-kamel

Copy link
Copy Markdown
Contributor Author

@Sameerlite done — rebased cleanly onto litellm_internal_staging. 🙏

The out-of-scope files came from the branch having been based on litellm_oss_staging, so the diff was carrying every commit that differs between the two staging branches (the README / Bedrock / vertex_ai / proxy_server reverts, etc.). I cherry-picked just the single OCI re-land commit onto the current litellm_internal_staging tip, so the diff is now only the 8 OCI Cohere-judge files (+332/-195):

  • litellm/llms/oci/chat/cohere.py, litellm/llms/oci/chat/transformation.py, litellm/llms/oci/common_utils.py
  • litellm/types/llms/oci.py
  • tests/test_litellm/llms/oci/chat/test_oci_cohere_tool_calls.py, .../test_oci_chat_transformation.py, .../test_oci_coverage_boost.py
  • tests/integration/test_oci_integration.py

No out-of-scope files remain. CI is re-running on the clean head (50169f3d). Should be ready now — thanks for the patience!

@fede-kamel
fede-kamel force-pushed the litellm_oci_cohere_judge_reland branch from 50169f3 to c25510d Compare July 13, 2026 20:45
@fede-kamel

Copy link
Copy Markdown
Contributor Author

@Sameerlite rebased onto the current litellm_internal_staging tip — the conflicts were purely from the black → ruff-format migrations (#31317, #31518) that landed after my last rebase; no semantic changes upstream to the OCI Cohere files.

The diff remains scoped to the same 8 OCI files as before. Verified locally against the rebased state:

  • ruff format --check clean, scripts/ruff_strict_gate.py and scripts/type_discipline_gate.py both within budget
  • Full OCI unit suite: 372 passed

Since you'd already reviewed the code as solid (Greptile 5/5, CI green) before the conflicts appeared, this should be ready to land whenever you get a chance. Thanks!

@codspeed

codspeed Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing fede-kamel:litellm_oci_cohere_judge_reland (02da067) with main (9fd78ff)

Open in CodSpeed

@fede-kamel

Copy link
Copy Markdown
Contributor Author

@Sameerlite rebased onto the current litellm_internal_staging tip again. The conflicts were from the lint sweeps that landed since July (the new LIT010 Final-on-locals rule, typing modernization, and the litellm/types ruff exclusion removal); there were no semantic changes to the OCI Cohere files upstream, so the fix itself is unchanged. The diff remains scoped to the same 8 OCI files.

Verified locally after the rebase: full OCI unit suite 372 passed, ruff format clean, and the strict, type discipline, and basedpyright gates all within budget. Should be ready to land whenever you get a chance

@fede-kamel

Copy link
Copy Markdown
Contributor Author

@greptileai please re-review; the rebase onto the current litellm_internal_staging tip adapted the fix to the new lint rules (Final on locals, modernized typing), no semantic changes.

Re-verified live against OCI GenAI in us-chicago-1 at f0b467b: the branch passes all four Cohere tool integration tests (array-param tools and tool-result continuations on cohere.command-latest and cohere.command-a-03-2025), while the same tests on the unpatched litellm_internal_staging tip fail 3 of 4 with the HTTP 500 and continuation errors this PR fixes, so the re-land is still needed

@fede-kamel

Copy link
Copy Markdown
Contributor Author

@greptileai review

@eriko13

eriko13 commented Aug 17, 2026 •

Copy link
Copy Markdown

Hi — we are blocked on this re-land for our OCI GenAI (me-riyadh-1) deployment: with the current main-stable / main-latest images, multi-turn chained tool-calling through the OCI/Cohere adapter fails on the continuation turn (OCI 400 "cannot specify message if the last entry in chat history contains tool results") because the transform still sends a populated top-level message with tool results in chatHistory instead of hoisting to top-level toolResults + empty message. We verified the raw OCI Signature-v1 shape (top-level toolResults) chains fine.

Since none of the published tags (stable/latest/v1.97.0/main-latest) carry this change, we would appreciate a release once it lands — we will re-verify immediately against our probe and confirm chained tool-calling works end-to-end. Happy to help test the re-land if useful.

@fede-kamel
fede-kamel force-pushed the litellm_oci_cohere_judge_reland branch from f0b467b to 2d4989f Compare August 17, 2026 19:21
@fede-kamel

Copy link
Copy Markdown
Contributor Author

Thanks for verifying the toolResults shape against the API. Rebased onto today's staging tip and live tests pass; release timing is up to the maintainers

@fede-kamel

Copy link
Copy Markdown
Contributor Author

@Sameerlite rebased onto the current staging tip with the diff still scoped to the OCI files. A user above is blocked on this

@fede-kamel

Copy link
Copy Markdown
Contributor Author

@greptileai please re-review; the rebase adapts to the current staging tip and renames one helper for the basedpyright budget, no semantic changes

@fede-kamel
fede-kamel force-pushed the litellm_oci_cohere_judge_reland branch from 2d4989f to 48cb695 Compare August 20, 2026 20:41
@fede-kamel

Copy link
Copy Markdown
Contributor Author

Rebased onto current staging and conflicts are resolved. Five of this PR's regression tests fail on staging without it, so the fix is still needed

@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:25
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 16:02
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 23, 2026 16:02
@fede-kamel
fede-kamel force-pushed the litellm_oci_cohere_judge_reland branch 3 times, most recently from 12980a9 to 02da067 Compare September 28, 2026 19:39
…ic tool-calling continuation)

Re-lands BerriAI#30646, which was rolled back in a batch stabilization revert
(commit 15a7c68), with the greptile-review empty-message guard folded in.

Three OCI Cohere transform bugs broke MLflow {{trace}} judges:

1. Tool parameter types: a JSON-schema `array` param mapped to the bare
   string "List" in OCI_JSON_TO_PYTHON_TYPES, and OCI's Cohere backend
   returns HTTP 500 on a bare "List". Mapped the collection types to the
   lowercase builtins list/dict.

2. Agentic tool-calling continuation (request side): after a tool runs,
   litellm pulled the last user message into the top-level `message` and
   emitted the tool result as a TOOL entry in chatHistory. OCI rejects that
   ("cannot specify message if the last entry in chat history contains tool
   results"), and an empty message alone is also rejected ("message must be
   at least 1 token long or tool results must be specified"). OCI carries
   the current turn's results in a dedicated top-level `toolResults` field.
   The transform now sends an empty message, keeps the user turn in
   chatHistory, and puts results in `toolResults`, matching the
   langchain-oracle reference. Tool results are no longer chatHistory entries.

3. Response parsing: tool-grounded answers carry citations with `documentIds`
   (camelCase) and no `document_ids`, which made the required
   CohereCitation.document_ids field sink the whole response parse. That
   field (and CohereSearchQuery.generation_id) is now optional.

Verified live against us-chicago-1 (cohere.command-a-03-2025 and
command-latest), single and multi-round tool loops. Adds unit regressions on
the transformed request shape and citation parsing, plus gated integration
tests for the continuation.
@fede-kamel
fede-kamel force-pushed the litellm_oci_cohere_judge_reland branch from 02da067 to 89d82ad Compare November 5, 2026 19:31
@fede-kamel

Copy link
Copy Markdown
Contributor Author

Rebased onto main at 88ea40e (head now 89d82ad). Conflicts in cohere.py, transformation.py and types/llms/oci.py (upstream extract_text_content rename, LiteLLMBaseModel, typing passes) resolved keeping this PR's shape; two new helpers now take/return Sequence for the type-discipline budget. Local, Python 3.12: pytest tests/unit/llms/oci (484 passed, 5 skipped), ruff check/format, ruff-strict/type-discipline/test-quality gates vs main, basedpyright on changed files (277→275) — all green; the failing misc shard was the unrelated interactions OpenAPI test. Overlaps #46157 and open PRs #46159/#46161/#46181 on the tool-result turn; unique here: the List/Dict parameter-type 500 fix, optional generation_id, and no TOOL chatHistory entries.

@fede-kamel

fede-kamel commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

@Sameerlite this re-lands #30646 (the Cohere {{trace}} judge fix you merged in June), which was rolled back in the batch stabilization revert 15a7c687 along with several unrelated PRs. It's up to date, CI is green, and five of its regression tests fail without it; a user in this thread is blocked on it for their OCI deployment. Could you or @mateo-berri take a look when you have a moment? Thanks!

@fede-kamel

Copy link
Copy Markdown
Contributor Author

@Sameerlite @yuneng-berri a nudge on this one: it re-lands #30646, the Cohere {{trace}} judge fix that @Sameerlite merged in June, which was rolled back in the batch stabilization revert 15a7c687 along with unrelated PRs. The diff is still scoped to the OCI files, the branch is mergeable against main and CI is green. Five of its regression tests fail on main without it, so OCI Cohere judges are still broken there. Could one of you take a look?

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.

4 participants