Skip to content

Conversation

ChinmayBansal
Copy link
Contributor

@ChinmayBansal ChinmayBansal commented Sep 12, 2025

Related Issues

Proposed Changes:

This PR implements reasoning support for Cohere's chat generator following the parent issue #9700 requirements. The implementation adds the ability to extract and store model reasoning content in the ReasoningContent field of ChatMessage.

Key Features Added:

  • Reasoning extraction function (_extract_reasoning_from_response) that
    supports multiple formats:
    • XML-style tags: <thinking>, <reasoning>
    • Section headers: ## Reasoning, ## Thinking, ## My reasoning
    • Natural language patterns: "Let me think", "Step by step", "First,"
    • Intelligent separator detection to determine reasoning boundaries
  • Dual-path response parsing that handles both regular responses and tool
    call scenarios
  • Streaming support with custom
    _convert_streaming_chunks_to_chat_message_with_reasoning function
  • Tool call compatibility ensuring reasoning works alongside tool calls
    without conflicts
  • Content cleaning that removes reasoning markers from the main response
    text
  • Length filtering with 30-character minimum threshold to avoid false
    positives

The implementation follows the established patterns from Ollama (#2200) and
Google GenAI (#2212) PRs, ensuring consistency across providers.

How did you test it?

  • Unit tests: Added comprehensive test suite (TestReasoningExtraction)
    covering:
    • Various reasoning pattern formats (<thinking>, <reasoning>,
      step-by-step)
    • Edge cases (no reasoning, short reasoning ignored)
    • Natural language reasoning detection
  • Integration tests: Added TestCohereChatGeneratorReasoning class with:
    • End-to-end reasoning extraction with Command A Reasoning model
      (lightweight)
    • Mock response testing for consistent behavior
    • Tool call compatibility verification
  • Manual verification: All unit tests pass, integration tests show expected
    rate limiting (API working correctly)
  • Code quality: All linting (hatch run fmt) and type checking (hatch run test:types) pass

Notes for the reviewer

  • The reasoning extraction logic is in _extract_reasoning_from_response()
    function (lines ~355-435)
  • Key integration points are in _parse_response() for both regular and tool
    call paths (lines ~194-220)
  • Streaming support uses a post-processing approach since Cohere embeds
    reasoning in response text
  • Test cases use the lightweight command-a-reasoning-111b-2024-10-03 model as
    recommended
  • Implementation handles the Cohere-specific thinking parameter via
    generation_kwargs={"thinking": True}

Checklist

  • I have read the contributors guidelines and the code of conduct
  • I have updated the related issue with new insights and changes
  • I added unit tests and updated the docstrings
  • I've used one of the conventional commit types for my PR title: fix:,feat:, build:, chore:, ci:, docs:,style:, refactor:, perf:,test:.

@ChinmayBansal ChinmayBansal requested a review from a team as a code owner September 12, 2025 06:32
@ChinmayBansal ChinmayBansal requested review from sjrl and removed request for a team September 12, 2025 06:32
@github-actions github-actions bot added integration:cohere type:documentation Improvements or additions to documentation labels Sep 12, 2025
@ChinmayBansal ChinmayBansal force-pushed the feat/cohere-reasoning-support branch from 9bce185 to 52ae02b Compare September 16, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration:cohere type:documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cohere - reasoning support
1 participant