Skip to content

feat(openai): add message_transform hook to OpenAI providers#298

Merged
programminx-askui merged 2 commits into
mainfrom
feat/openai-message-transform
Jul 23, 2026
Merged

feat(openai): add message_transform hook to OpenAI providers#298
programminx-askui merged 2 commits into
mainfrom
feat/openai-message-transform

Conversation

@programminx-askui

Copy link
Copy Markdown
Collaborator

What & why

Some OpenAI-compatible gateways deviate from the stock OpenAI chat spec (stricter message-ordering, content=null requirements, etc.). Today the only way to adapt the request payload is to monkeypatch the internal _to_openai_messages conversion function — private, process-global, and fragile across upgrades.

This adds a first-class, per-provider hook.

Changes

  • OpenAIMessagesApi gains a message_transform parameter. When set, it is applied to the OpenAI-format messages list immediately after conversion and before chat.completions.create(...).
  • Threaded through OpenAIVlmProvider and OpenAICompatibleVlmProvider (constructor param + docstrings).
  • New public MessageTransform type alias (Callable[[list[dict]], list[dict]]), exported from askui.model_providers.
  • Test: tests/unit/models/openai/test_message_transform.py (transform applied vs. pass-through).

Backward compatible — defaults to None, messages sent unchanged.

Usage

from askui.model_providers import OpenAICompatibleVlmProvider

def normalize(messages: list[dict]) -> list[dict]:
    ...  # adapt to the gateway's rules
    return messages

provider = OpenAICompatibleVlmProvider(
    endpoint_url="https://my-gateway/v2/chat/completions",
    model_id="gpt-5.4",
    message_transform=normalize,
)

🤖 Generated with Claude Code

Add an optional `message_transform` callback to OpenAIMessagesApi,
OpenAIVlmProvider and OpenAICompatibleVlmProvider. It post-processes the
OpenAI-format `messages` list right before the request is sent, letting
callers adapt to OpenAI-compatible gateways that deviate from the stock
chat spec (e.g. stricter message-ordering or content rules) without
monkeypatching internal conversion functions.

Backward compatible: defaults to None (messages sent unchanged). Exposes a
`MessageTransform` type alias from askui.model_providers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@programminx-askui
programminx-askui merged commit eaf274e into main Jul 23, 2026
1 check passed
@programminx-askui
programminx-askui deleted the feat/openai-message-transform branch July 23, 2026 18:32
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