Skip to content

finish reason mapping part 2 #1214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

narengogi
Copy link
Collaborator

@narengogi narengogi commented Jul 7, 2025

Status of finish reason mapping by provider (only mentioning providers which are not openai compliant)

provider status comments
ai21 the integration needs to be updated, raised as a separate issue #1215
anthropic -
bedrock - converse -
bedrock - cohere Cohere integration needs to be updated, raised as a separate issue
bedrock - ai21 integration needs to be updated, raised as a separate issue
bedrock - titan -
cohere integration needs to be updated #321
deepseek -
mistral -
together-ai -

Copy link

matter-code-review bot commented Jul 7, 2025

Code Quality new feature refactoring

Summary By MatterAI MatterAI logo

🔄 What Changed

This pull request, titled "finish reason mapping part 2", continues the effort to standardize finish_reason mapping logic across various Large Language Model (LLM) providers to ensure better compliance with OpenAI's API standards. The specific change in this patch involves reformatting an import statement in src/providers/anthropic/complete.ts for improved readability. The broader context of "finish reason mapping part 2" from previous changes includes introducing new TypeScript enum types for provider-specific finish reasons (DeepSeek, Google, Mistral AI, Together AI, and Bedrock Titan), renaming an existing Bedrock enum, and updating the PROVIDER_FINISH_REASON union type. The core functionality involves integrating a transformFinishReason utility function into the response transformation logic of Anthropic, Bedrock, DeepSeek, Google, Google Vertex AI, Mistral AI, and Together AI providers to standardize stop_reason or completionReason values into a unified FINISH_REASON enum.

🔍 Impact of the Change

This change improves the interoperability and consistency of finish_reason values across different LLM providers, making it easier for downstream consumers to handle responses uniformly. It enhances the platform's ability to abstract away provider-specific nuances, leading to a more robust and maintainable codebase. The strictOpenAiCompliance flag provides flexibility for applications that require strict adherence to OpenAI's finish_reason definitions. The specific formatting change in this PR improves code readability.

📁 Total Files Changed

  • src/providers/anthropic/complete.ts: Modified to use transformFinishReason for stop_reason mapping and import formatting.
  • src/providers/bedrock/complete.ts: Modified to use transformFinishReason for completionReason mapping and TITAN_COMPLETION_REASON enum.
  • src/providers/bedrock/types.ts: Renamed BEDROCK_STOP_REASON to BEDROCK_CONVERSE_STOP_REASON and added TITAN_STOP_REASON enum.
  • src/providers/deepseek/chatComplete.ts: Modified to use transformFinishReason for finish_reason mapping and DEEPSEEK_STOP_REASON enum.
  • src/providers/deepseek/types.ts: Added DEEPSEEK_STOP_REASON enum.
  • src/providers/google-vertex-ai/chatComplete.ts: Modified to use transformFinishReason for finishReason mapping.
  • src/providers/google-vertex-ai/types.ts: Updated finishReason type and added VERTEX_GEMINI_GENERATE_CONTENT_FINISH_REASON enum.
  • src/providers/google/chatComplete.ts: Modified to use transformFinishReason for finishReason mapping.
  • src/providers/google/types.ts: Added GOOGLE_GENERATE_CONTENT_FINISH_REASON enum.
  • src/providers/mistral-ai/chatComplete.ts: Modified to use transformFinishReason for finish_reason mapping and MISTRAL_AI_FINISH_REASON enum.
  • src/providers/mistral-ai/types.ts: Added MISTRAL_AI_FINISH_REASON enum.
  • src/providers/together-ai/chatComplete.ts: Modified to use transformFinishReason for finish_reason mapping and fixed a bug in stream chunk transformation.
  • src/providers/together-ai/types.ts: Added TOGETHER_AI_FINISH_REASON enum.
  • src/providers/types.ts: Updated PROVIDER_FINISH_REASON union type to include all new and renamed enums.
  • src/providers/utils/finishReasonMap.ts: Expanded finishReasonMap with mappings for all newly integrated provider finish reasons.

🧪 Test Added

N/A. No specific tests were mentioned or added in the provided patch.

🔒Security Vulnerabilities

No security vulnerabilities were detected in the provided code patch. The changes primarily involve data transformation and type safety, without introducing new external inputs or insecure practices.

Motivation

This change is part of an ongoing effort to standardize API responses across various LLM providers, specifically for finish_reason, to enhance interoperability and consistency with OpenAI's API standards.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests
  • Integration Tests
  • Manual Testing

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Tanka Poem ♫

Diverse reasons flow,
Unified now, a clear design.
Order from chaos,
Code's beauty, a scientist's dream,
API's language, now one. 🌟

Sequence Diagram

sequenceDiagram
    participant Provider as LLM Provider (e.g., Anthropic)
    participant TransformReason as transformFinishReason Utility
    participant UnifiedResponse as Unified API Response

    Provider->>Provider: Receive LLM Response
    Provider->>Provider: Extract Provider-Specific Finish Reason (e.g., stop_reason, completionReason)
    Provider->>TransformReason: Call transformFinishReason(providerReason, strictOpenAiCompliance)
    TransformReason->>TransformReason: Map providerReason to FINISH_REASON enum
    TransformReason-->>Provider: Return unified FINISH_REASON
    Provider->>UnifiedResponse: Include unified FINISH_REASON in response
Loading

Copy link

@matter-code-review matter-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks good overall. It standardizes finish reason handling across different AI providers by mapping provider-specific finish reason codes to a common set of standardized reasons. I have one suggestion for error handling improvement.

Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

3 similar comments
Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@narengogi narengogi marked this pull request as ready for review July 7, 2025 10:51
@narengogi narengogi requested review from VisargD and sk-portkey July 7, 2025 10:51
@narengogi narengogi changed the title Chore/finish reason mapping part 2 finish reason mapping part 2 Jul 7, 2025
@narengogi narengogi requested a review from b4s36t4 July 7, 2025 11:18
Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@narengogi narengogi linked an issue Jul 9, 2025 that may be closed by this pull request
Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@VisargD
Copy link
Collaborator

VisargD commented Aug 5, 2025

@narengogi Can you please resolve conflicts?

@narengogi narengogi force-pushed the chore/finish-reason-mapping-part-2 branch from 75d024a to 3bcb84f Compare August 5, 2025 16:31
Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

stop reason for google

bedrock titan finish reason fix

fix finish reason for deepseek

fix finish reason for mistral

fix finish reason for together ai

changes from self review

fix finish reason for anthropic completions
@narengogi narengogi force-pushed the chore/finish-reason-mapping-part-2 branch from 3bcb84f to 56b62e1 Compare August 5, 2025 16:50
Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Copy link

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

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.

Google Gemini stop reason not handled with strictOpenAICompliance: true Translation of Stop Reason for Anthropic -> OpenAI
2 participants