Skip to content

fix: modernize Cohere generator (#1384)#1673

Open
HayatoFujihara wants to merge 4 commits intoNVIDIA:mainfrom
HayatoFujihara:fix/update-cohere-generator
Open

fix: modernize Cohere generator (#1384)#1673
HayatoFujihara wants to merge 4 commits intoNVIDIA:mainfrom
HayatoFujihara:fix/update-cohere-generator

Conversation

@HayatoFujihara
Copy link
Copy Markdown
Contributor

@HayatoFujihara HayatoFujihara commented Apr 5, 2026

Summary

Fixes #1384

Addresses all seven checklist items in #1384.

  • Drop default model: name parameter is now required; passing no model name raises TargetNameMissingError. The deprecated command default has been removed.
  • Cancel run on 404: Both v1 and v2 API paths now catch cohere.errors.NotFoundError and raise BadGeneratorException to stop execution immediately, following the pattern in nvcf.py.
  • Migrate to cohere.errors: Replaced the broad RuntimeError catch in the v1 path with fine-grained cohere.errors types (NotFoundError, GatewayTimeoutError, TooManyRequestsError, ServiceUnavailableError, InternalServerError). The v2 path already used these.
  • Live endpoint tests (chat): Added test_live_chat_api_output_format — verifies that the v2 chat API returns a Message object with non-empty text. Skipped when COHERE_API_KEY is not set.
  • Live endpoint tests (completion): Added test_live_v1_api_rejects_new_model — Cohere has retired the generate endpoint and all current models return 404 on v1, so the test verifies that the 404 handling correctly raises BadGeneratorException rather than silently returning None.
  • Preserve Nones: Error paths continue to emit None per the existing protocol. _call_model wraps str results in Message while passing None through unchanged.
  • _load_client pattern: Extracted client initialization into _load_client(), called from __init__ and __setstate__. Added _unsafe_attributes = ["generator"] so pickle round-trips correctly restore the client.

Test plan

  • pytest tests/generators/test_cohere.py — 17 tests (15 mock + 2 live)
  • ruff check garak/generators/cohere.py tests/generators/test_cohere.py
  • Live tests require COHERE_API_KEY; safely skipped in CI when absent

HayatoFujihara and others added 2 commits April 5, 2026 21:18
- Extract _load_client() for client initialization, supporting
  pickle serialization/deserialization via __setstate__
- Remove deprecated 'command' model default; model name is now required
- Add 404 (NotFoundError) handling for both v1 and v2 API paths,
  raising BadGeneratorException to stop execution immediately
- Migrate v1 error handling from RuntimeError to cohere.errors
- Wrap string results in Message objects in _call_model for type
  consistency with base Generator contract
- Update tests: model name required, _load_client, serialization,
  404 handling, Message type verification

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Add live chat API test verifying Message type and non-empty text
- Add live v1 API test verifying 404 handling for newer models
- Tests are skipped when COHERE_API_KEY is not set (CI-safe)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@jmartin-tech jmartin-tech left a comment

Choose a reason for hiding this comment

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

This is a good start on modernization.

A few asks from the issue have drifted from the core design requiring a bit of guideance here.

Also the generator currently accepts configuration to support use of the legacy v1 client via the legacy code in self.cohere.Client. #1673 is also linked to #1323 denoting a desire to have self.cohere.ClientV2 be used for all inference requests. Consider evaluating the scope that additional change to determine if it can be incorporated easily with this PR.

Comment thread garak/generators/cohere.py Outdated
Comment thread garak/generators/cohere.py Outdated
Comment thread tests/generators/test_cohere.py Outdated
HayatoFujihara and others added 2 commits April 7, 2026 22:42
…move __setstate__, use config_root in tests (NVIDIA#1384)

- Rename _load_client() to _load_unsafe() to follow Configurable framework convention
- Remove redundant __setstate__ override (Configurable.__setstate__ auto-calls _load_unsafe)
- Replace direct name= constructor arg with config_root dict pattern in all tests
- Add cohere_config_root and cohere_v1_config_root test fixtures

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
, NVIDIA#1384)

- Remove legacy cohere.Client and generate() API path entirely
- Unify all requests to use cohere.ClientV2 with chat() API
- Remove COHERE_GENERATION_LIMIT and sub-batching in _call_model
  (base class generate() handles iteration for supports_multiple_generations=False)
- Remove v1-only parameters (stop, preset, api_version)
- Add DeprecationWarning when api_version is set in config
- Remove v1-specific tests and fixtures, add deprecation warning test

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@HayatoFujihara
Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review! I've addressed all feedback in two commits:

Commit 1 (6c19a75): Review feedback

  • Renamed _load_client()_load_unsafe() to follow Configurable framework convention
  • Removed redundant __setstate__ override
  • Switched all tests to use config_root dict pattern with shared fixtures

Commit 2 (5f461af): v1 removal per #1323

  • Removed legacy cohere.Client / generate() path entirely, unified to ClientV2
  • Removed COHERE_GENERATION_LIMIT and sub-batching (base class handles iteration for supports_multiple_generations=False)
  • Removed v1-only params (stop, preset, api_version) — api_version now emits a DeprecationWarning if set
  • All 13 tests pass

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.

generator: update cohere

2 participants