feat(openai): add message_transform hook to OpenAI providers#298
Merged
Conversation
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>
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.
What & why
Some OpenAI-compatible gateways deviate from the stock OpenAI chat spec (stricter message-ordering,
content=nullrequirements, etc.). Today the only way to adapt the request payload is to monkeypatch the internal_to_openai_messagesconversion function — private, process-global, and fragile across upgrades.This adds a first-class, per-provider hook.
Changes
OpenAIMessagesApigains amessage_transformparameter. When set, it is applied to the OpenAI-formatmessageslist immediately after conversion and beforechat.completions.create(...).OpenAIVlmProviderandOpenAICompatibleVlmProvider(constructor param + docstrings).MessageTransformtype alias (Callable[[list[dict]], list[dict]]), exported fromaskui.model_providers.tests/unit/models/openai/test_message_transform.py(transform applied vs. pass-through).Backward compatible — defaults to
None, messages sent unchanged.Usage
🤖 Generated with Claude Code