Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/genkit-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ alias.
On `gpt-image-2` and older GPT Image models they fail before the request is
sent. To send one anyway, pass it as `extra={'quality': 'xhigh'}`.

## Structured output

OpenAI only accepts an object at the root of an output schema, so `OpenAI()`
sends any other schema (a list, enum or scalar, such as `output_schema=int`,
a `str` Enum or `{'type': 'array', 'items': {'type': 'string'}}`) as the
`value` property of an object and unwraps the reply. `response.output` and
the streamed chunks hold the value itself. Requests to Mistral are sent
unwrapped.

## Mistral

`Mistral()` registers Mistral's models and embedders under `mistral/`, as the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_model_handler(
raise ValueError(f"Model '{model}' is not supported.")

info = _SUPPORTED_MODELS[model]
openai_model = OpenAIModel(model, client, supports=info.supports)
openai_model = OpenAIModel(model, client, supports=info.supports, object_schema_root=True)
return cls(openai_model).generate

async def generate(self, request: ModelRequest, ctx: ModelContext) -> ModelResponse:
Expand Down
79 changes: 75 additions & 4 deletions packages/genkit-openai/src/genkit_openai/_models/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
from typing import Any, cast

from genkit_openai._models._mistral import is_mistral, mistral_chat_body, refuse_non_image_media
from genkit_openai._models._output_root import (
OutputStreamUnwrapper,
has_object_root,
unwrap_output_text,
wrap_schema_root,
)
from genkit_openai._models._utils import (
OPENAI_CALL_ERRORS,
DictMessageAdapter,
Expand Down Expand Up @@ -272,6 +278,9 @@ def _root_ref_target(schema: dict[str, Any]) -> dict[str, Any] | None:
def _is_wrapped_tool_schema(schema: dict[str, Any] | None) -> bool:
"""True for a non-empty schema that is not an object (``{'type': 'string'}``).

OpenAI takes only an object for a function's parameters, so such a
schema is sent wrapped.

A root ``$ref`` is judged by its target: Pydantic emits one for an Enum
input, which is a string, not an object.
"""
Expand Down Expand Up @@ -341,6 +350,30 @@ def _unwrapped_args_text(arguments: str) -> str:
return arguments


def _unwrap_output(message: Message) -> Message:
"""Replace a wrapped reply's text with the wrapped value.

The text parts are read as one text, as a streamed reply arrives in many
of them, and the value goes in the first. Text beside a tool call is a
preamble, not the output, and is left alone.
"""
if any(part.tool_request is not None for part in message.content):
return message
texts = [i for i, part in enumerate(message.content) if part.text is not None]
if not texts:
return message
text = ''.join(message.content[i].text or '' for i in texts)
value = unwrap_output_text(text)
if value == text:
return message
content = [
part.model_copy(update={'text': value}) if i == texts[0] else part
for i, part in enumerate(message.content)
if i == texts[0] or i not in texts
]
return message.model_copy(update={'content': content})


def _wrap_tool_inputs(messages: list[Message], wrapped: frozenset[str]) -> list[Message]:
"""Replay earlier calls to scalar-input tools in the wrapped wire shape."""
return [_map_tool_requests(m, wrapped, wrap=True) for m in messages]
Expand All @@ -361,6 +394,7 @@ def __init__(
*,
supports: ModelSupports | None = None,
mistral: bool | None = None,
object_schema_root: bool = False,
) -> None:
"""Initializes the OpenAIModel instance with the specified model and OpenAI client parameters.

Expand All @@ -373,11 +407,16 @@ def __init__(
``json_object`` so a fine-tune keeps working.
mistral: Send Mistral's field names (see ``_mistral.py``).
``None`` decides from the client's host.
object_schema_root: The host takes a ``json_schema`` response
format only with an object root (OpenAI). Any other output
schema is sent as the ``value`` property of an object, and
the reply is unwrapped.
"""
self._model = model
self._openai_client = client
self._supports = supports
self._mistral = is_mistral(client) if mistral is None else mistral
self._object_schema_root = object_schema_root

@property
def name(self) -> str:
Expand Down Expand Up @@ -461,6 +500,16 @@ def _needs_schema_in_prompt(self, request: ModelRequest) -> bool:
# DeepSeek models use json_object mode — schema never reaches the API.
return self._model.startswith('deepseek')

def _wraps_output(self, request: ModelRequest) -> bool:
"""Whether the output schema goes out wrapped, so the reply must be unwrapped."""
return (
self._object_schema_root
and request.output_format == 'json'
and not self._model.startswith('deepseek')
and bool(request.output_schema)
and not has_object_root(request.output_schema)
)

def _get_response_format(self, request: ModelRequest) -> dict | None:
"""Determines the response format configuration based on the output settings.

Expand All @@ -481,13 +530,14 @@ def _get_response_format(self, request: ModelRequest) -> dict | None:
if self._model.startswith('deepseek'):
return {'type': 'json_object'}
if request.output_schema:
schema = request.output_schema
if self._wraps_output(request):
schema = wrap_schema_root(schema)
return {
'type': 'json_schema',
'json_schema': {
'name': request.output_schema.get('title', 'Response'),
'schema': _ensure_strict_json_schema(
request.output_schema, path=(), root=request.output_schema
),
'schema': _ensure_strict_json_schema(schema, path=(), root=schema),
'strict': True,
},
}
Expand Down Expand Up @@ -650,6 +700,8 @@ async def _generate(self, request: ModelRequest) -> ModelResponse:
)

genkit_message = _unwrap_tool_inputs(MessageConverter.to_genkit(message), _wrapped_tool_names(request.tools))
if self._wraps_output(request):
genkit_message = _unwrap_output(genkit_message)
# An empty message is the answer when the model was blocked or cut off; at a clean stop it is unreadable.
if not genkit_message.content and finish_reason is FinishReason.STOP:
raise ValueError('Unable to determine content part')
Expand Down Expand Up @@ -694,6 +746,10 @@ async def _generate_stream(

tool_calls: dict[int, Any] = {}
wrapped = _wrapped_tool_names(request.tools)
# A wrapped reply streams as '{"value": ...}'; chunks are unwrapped as
# they arrive. The final message is built from the raw text and
# unwrapped as a full reply is, so the two paths can't disagree.
unwrapper = OutputStreamUnwrapper() if self._wraps_output(request) else None
# As a full reply is read (MessageConverter.to_genkit): reasoning from
# its own field (DeepSeek's reasoning_content) first, then the
# content's reasoning and text in the order they arrived (Mistral's
Expand Down Expand Up @@ -735,10 +791,19 @@ async def _generate_stream(
field_reasoning.append(reasoning_part)
parts.append(reasoning_part)

# Text before a tool call is a preamble, not the output: whatever
# was held back goes out as it streamed, and so does the rest.
if unwrapper is not None and delta.tool_calls and (held_text := unwrapper.pass_through()):
Comment thread
adesinah marked this conversation as resolved.
Resolved
parts.append(MessageConverter.text_part_to_genkit(held_text))

# Text content; Mistral's reasoning models stream reasoning in it too.
for item in reply_content(delta.content):
part = MessageConverter.text_part_to_genkit(item) if isinstance(item, str) else item
content_parts.append(part)
if isinstance(item, str) and unwrapper is not None:
if chunk_text := unwrapper.push(item):
parts.append(MessageConverter.text_part_to_genkit(chunk_text))
continue
parts.append(part)

# Tool calls (partial function calls)
Expand All @@ -761,6 +826,9 @@ async def _generate_stream(
if parts:
callback(ModelResponseChunk(role=Role.MODEL, content=parts))

if unwrapper is not None and (rest := unwrapper.flush()):
callback(ModelResponseChunk(role=Role.MODEL, content=[MessageConverter.text_part_to_genkit(rest)]))

if not saw_choice:
raise GenkitError(
status='INTERNAL',
Expand Down Expand Up @@ -798,9 +866,12 @@ async def _generate_stream(
failure_message=failure_message,
)

final_message = Message(role=Role.MODEL, content=accumulated_content)
if unwrapper is not None:
final_message = _unwrap_output(final_message)
result = ModelResponse(
request=request,
message=Message(role=Role.MODEL, content=accumulated_content),
message=final_message,
finish_reason=finish_reason,
finish_message=finish_message,
usage=_usage_from_completion(usage),
Expand Down
Loading
Loading