Skip to content

Add moderation API support to gateway provider#92

Open
tbille wants to merge 6 commits intomainfrom
add-moderation-api-support
Open

Add moderation API support to gateway provider#92
tbille wants to merge 6 commits intomainfrom
add-moderation-api-support

Conversation

@tbille
Copy link
Copy Markdown
Contributor

@tbille tbille commented Apr 17, 2026

Summary

Adds content moderation support to the Go SDK's gateway provider, exposing the gateway's POST /v1/moderations endpoint through a new ModerationProvider interface. Introduces *errors.UnsupportedError so callers can cleanly detect backends (e.g. Anthropic) that do not offer moderation.

Changes

  • Add ModerationParams, ModerationResult, ModerationResponse, ContentPart, ImageURL and the optional ModerationProvider interface in providers/types.go; add Moderation bool to providers.Capabilities.
  • Add UnsupportedError, ErrUnsupported sentinel, CodeUnsupported constant, and NewUnsupportedError constructor in errors/errors.go to distinguish "provider does not support this operation" from the existing "provider not registered" case.
  • Re-export the new types, interface, and sentinel from the root anyllm package to match the existing alias pattern.
  • Implement Moderation on *gateway.Provider by calling POST /v1/moderations directly with the captured *http.Client, so IncludeRaw can be forwarded as ?include_raw=true and the gateway's locked "does not support moderation" / "multimodal moderation" 400 phrasing can be mapped to *UnsupportedError. Capture baseURL, httpClient and the platform token on Provider at construction so direct HTTP calls share the same auth as the embedded openai-go SDK. Advertise Moderation: true in capabilities() and add the providers.ModerationProvider interface assertion.
  • Add httptest-based tests covering the happy path, the include_raw query parameter, both unsupported phrasings (moderation / multimodal) mapping to *UnsupportedError, status-code mapping for 401/402/429/502/504, platform and non-platform auth on the direct HTTP call, and an env-gated integration test.
  • Add a README snippet showing the gateway provider's Moderation method and how to branch on UnsupportedError.

Testing

  • go test ./errors ./providers/gateway -race -count=1 (targeted to packages changed)
  • Integration test TestIntegrationModeration is env-gated on GATEWAY_API_BASE / GATEWAY_PLATFORM_TOKEN and skips when unset.
  • Full suite runs in CI.

Checklist

  • Linting passes (make lint)
  • Tests pass locally (make test)
  • Documentation updated (if needed)
  • No breaking changes (or documented in summary)

tbille added 6 commits April 17, 2026 15:54
Introduce ModerationParams, ModerationResult, and ModerationResponse to
support content moderation via the gateway provider. Add
ModerationProvider as an optional interface and a Moderation bool on
Capabilities so callers can discover support at runtime.
UnsupportedError distinguishes 'provider does not support this operation'
(e.g. moderation on Anthropic) from the existing UnsupportedProviderError
(provider not registered at all). Carries the operation name so callers
can branch on what was refused.
Match the existing re-export pattern so callers can use anyllm.ModerationParams,
anyllm.ErrUnsupported, etc. without importing the providers and errors
subpackages directly.
Call POST /v1/moderations directly via the captured HTTP client so the
include_raw query parameter and gateway-specific 'does not support
moderation' 400 mapping to *errors.UnsupportedError are both under our
control instead of the openai-go SDK's. Capture baseURL, httpClient and
platform token on Provider so direct HTTP calls honour the same auth the
embedded SDK uses, and advertise Moderation in Capabilities.
Cover the happy path, include_raw query parameter, the two unsupported
phrasings that map to *UnsupportedError, status-code-to-error mapping for
401/402/429/502/504, platform and non-platform auth on the direct HTTP
call, and an env-gated integration test.
Show the gateway provider's Moderation method and how to branch on
UnsupportedError when the backend model has no moderation support.
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.

1 participant