feat: add FinishReason enum on server side#996
Open
VedantMadane wants to merge 1 commit intocohere-ai:mainfrom
Open
feat: add FinishReason enum on server side#996VedantMadane wants to merge 1 commit intocohere-ai:mainfrom
VedantMadane wants to merge 1 commit intocohere-ai:mainfrom
Conversation
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
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a type-safe
FinishReasonenum 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
FinishReasonenum tobackend/chat/enums.pywith values:ERROR,COMPLETE,MAX_TOKENSbackend/schemas/chat.pyto useFinishReasontype instead ofstrfor:ChatResponse.finish_reason(Optional[FinishReason])NonStreamedChatResponse.finish_reason(FinishReason)"ERROR"withFinishReason.ERRORinbackend/chat/custom/custom.pyFinishReason.MAX_TOKENSinstead of string literalFrontend
src/interfaces/assistants_web/src/cohere-client/constants.tswith TODO comment to use generated types after runningmake generate-client-webImplementation Details
The
FinishReasonenum follows the same pattern as the existingStreamEventenum in the codebase, using Python'sStrEnumfor compatibility with string-based APIs while providing type safety.Files Modified
src/backend/chat/enums.py- Added FinishReason enumsrc/backend/schemas/chat.py- Updated type annotationssrc/backend/chat/custom/custom.py- Replaced string constantsrc/backend/tests/unit/conftest.py- Updated test fixturessrc/backend/tests/integration/conftest.py- Updated test fixturessrc/backend/tests/unit/routers/test_chat.py- Updated assertionssrc/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 generationTesting
Next Steps
After this PR is merged, run
make generate-client-webto generate TypeScript types from the backend schema. The frontend can then remove the localFinishReasonenum and use the generated types.Related Issue
Closes #914