Skip to content

feat: add FinishReason enum on server side#996

Open
VedantMadane wants to merge 1 commit intocohere-ai:mainfrom
VedantMadane:feat/add-finish-reason-enum-server-side
Open

feat: add FinishReason enum on server side#996
VedantMadane wants to merge 1 commit intocohere-ai:mainfrom
VedantMadane:feat/add-finish-reason-enum-server-side

Conversation

@VedantMadane
Copy link

Summary

Adds a type-safe FinishReason enum on the server side and replaces all string constants with the enum throughout the codebase. This improves type safety and ensures consistency across the backend and frontend.

Changes

Backend

  • Added FinishReason enum to backend/chat/enums.py with values: ERROR, COMPLETE, MAX_TOKENS
  • Updated backend/schemas/chat.py to use FinishReason type instead of str for:
    • ChatResponse.finish_reason (Optional[FinishReason])
    • NonStreamedChatResponse.finish_reason (FinishReason)
  • Replaced string constant "ERROR" with FinishReason.ERROR in backend/chat/custom/custom.py
  • Updated all mock deployments to use FinishReason.MAX_TOKENS instead of string literal
  • Updated test files to use enum values and assertions

Frontend

  • Updated src/interfaces/assistants_web/src/cohere-client/constants.ts with TODO comment to use generated types after running make generate-client-web

Implementation Details

The FinishReason enum follows the same pattern as the existing StreamEvent enum in the codebase, using Python's StrEnum for compatibility with string-based APIs while providing type safety.

Files Modified

  • src/backend/chat/enums.py - Added FinishReason enum
  • src/backend/schemas/chat.py - Updated type annotations
  • src/backend/chat/custom/custom.py - Replaced string constant
  • src/backend/tests/unit/conftest.py - Updated test fixtures
  • src/backend/tests/integration/conftest.py - Updated test fixtures
  • src/backend/tests/unit/routers/test_chat.py - Updated assertions
  • src/backend/tests/unit/model_deployments/mock_deployments/*.py - Updated mock responses (4 files)
  • src/interfaces/assistants_web/src/cohere-client/constants.ts - Added TODO for type generation

Testing

  • Enum imports successfully
  • All enum values are correct (ERROR, COMPLETE, MAX_TOKENS)
  • No linter errors in modified files
  • Existing tests updated to use enum values

Next Steps

After this PR is merged, run make generate-client-web to generate TypeScript types from the backend schema. The frontend can then remove the local FinishReason enum and use the generated types.

Related Issue

Closes #914

Move FinishReason enum to backend (server side) and replace all string constants with type-safe enum values.

Changes:

- Add FinishReason enum to backend/chat/enums.py

- Update schemas/chat.py to use FinishReason type

- Replace string constants with enum in chat/custom/custom.py

- Update all test files to use FinishReason enum values

- Update client-side constants.ts with TODO to use generated types after running make generate-client-web

Closes cohere-ai#914
@cla-assistant
Copy link

cla-assistant bot commented Jan 26, 2026

CLA assistant check
All committers have signed the CLA.

@cla-assistant
Copy link

cla-assistant bot commented Jan 26, 2026

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.

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.

Add FinishReason enum on server side

1 participant