fix(cohere): cap stop_sequences at five entries and omit null
PC
7h ago+85+85+01234567890123456789-4-4-0123456789
Description
Cohere's /v2/chat endpoint rejects requests with more than 5 stop_sequences with HTTP 400. Previously, internal/providers/cohere/chat.go forwarded stop and stop_sequences without capping the list length, and treated a JSON null value for stop as "" (serializing stop_sequences: [""]) or forwarded stop_sequences: null literally.
Root cause
stopSequencesininternal/providers/cohere/chat.godid not check for JSONnullbefore unmarshaling into astring(in Go, unmarshalingnullintostringsucceeds with""), and did not cap array length to Cohere's 5-sequence limit.copyChatExtraFieldsbypassedstopSequenceswhen falling back tostop_sequences.
Fix
- Omit
stop_sequenceswhenstoporstop_sequencesis empty or JSONnull, allowingstop: nullto fall back tostop_sequencesif provided. - Truncate
stop/stop_sequencesarrays longer than 5 entries tomaxStopSequences = 5and log a warning (slog.Warn), matchingproviders.CapStopSequencesand the Gemini provider (#1591). - Pass
stop_sequencesthroughstopSequencesas well.
Testing
- Added
TestChatCompletion_StopSequencesininternal/providers/cohere/cohere_test.gocovering within-limit lists, over-limit truncation for bothstopandstop_sequences, bare string wrapping,nullomission, andstop: nullfallback tostop_sequences. - Verified
go test ./internal/providers/cohere/...andgo test ./internal/testconventions/...pass.
Summary by CodeRabbit
- Bug Fixes
- Stop sequences now handle
nullvalues consistently, support a single string, and use the fallback field when the primary value isnull. - Requests with more than five stop sequences are limited to five.
- Stop sequences now handle