Skip to content

Conversation

@xcpky
Copy link
Contributor

@xcpky xcpky commented Dec 17, 2025

closes #3490

Add a field "inject_schema_instructions_in_native_mode" in ModelProfile

The implementation is basically as per @DouweM , with the following improvement:

NativeOutput now can accept a template and json schema instruction is injected regardless of inject_schema_instructions_in_native_mode in that case. For example:

result = await agent.run("how can I solve 8x + 7 = -23", output_type=NativeOutput(MathReasoning, template="{schema}"))

Copy link
Collaborator

@DouweM DouweM left a comment

Choose a reason for hiding this comment

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

@xcpky Thanks! Can you please add a test for the model profile behavior + new template field? If we update OutlinesModel/Provider to use the new model profile field, it may be sufficient to add a test to test_outlines.py.

template: str | None
"""Template for the prompt passed to the model.
The '{schema}' placeholder will be replaced with the output JSON schema.
If not specified, the default template specified on the model's profile will be used.
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to clarify that this is only used if 1) explicitly provided or 2) the model requires it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added more clarification.

"""
)
"""The instructions template to use for prompted structured output. The '{schema}' placeholder will be replaced with the JSON schema for the output."""
inject_schema_instructions_in_native_mode: bool = False
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have a mild preference for describing traits of the models/providers, rather than our behavior:

Suggested change
inject_schema_instructions_in_native_mode: bool = False
json_schema_output_requires_schema_in_instructions: bool = False

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we we set this to True on any profiles already?

Copy link
Collaborator

Choose a reason for hiding this comment

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

we may be able to remove this:

def customize_request_parameters(self, model_request_parameters: ModelRequestParameters) -> ModelRequestParameters:
"""Customize the model request parameters for the model."""
if model_request_parameters.output_mode in ('auto', 'native'):
# This way the JSON schema will be included in the instructions.
return replace(model_request_parameters, output_mode='prompted')
else:
return model_request_parameters

if we set the new field here:

def model_profile(self, model_name: str) -> ModelProfile | None:
"""The model profile for the named model, if available."""
return ModelProfile(
supports_tools=False,
supports_json_schema_output=True,
supports_json_object_output=True,
default_structured_output_mode='native',
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have a mild preference for describing traits of the models/providers, rather than our behavior:

I think native_output_requires_schema_in_instructions is better

we may be able to remove this:

def customize_request_parameters(self, model_request_parameters: ModelRequestParameters) -> ModelRequestParameters:
"""Customize the model request parameters for the model."""
if model_request_parameters.output_mode in ('auto', 'native'):
# This way the JSON schema will be included in the instructions.
return replace(model_request_parameters, output_mode='prompted')
else:
return model_request_parameters

if we set the new field here:

def model_profile(self, model_name: str) -> ModelProfile | None:
"""The model profile for the named model, if available."""
return ModelProfile(
supports_tools=False,
supports_json_schema_output=True,
supports_json_object_output=True,
default_structured_output_mode='native',
)

Done

Copy link
Collaborator

@DouweM DouweM left a comment

Choose a reason for hiding this comment

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

@xcpky Thanks! Can you please add a test for the model profile behavior + new template field? If we update OutlinesModel/Provider to use the new model profile field, it may be sufficient to add a test to test_outlines.py.

@xcpky
Copy link
Contributor Author

xcpky commented Dec 18, 2025

a

I've added the test, plz review.

@xcpky xcpky requested a review from DouweM December 18, 2025 07:00
Comment on lines +170 to +171
If not specified, the default template specified on the model's profile will be used only if `native_output_requires_schema_in_instructions` on the profile is True.
If explicitly provided, it's used regardless of `native_output_requires_schema_in_instructions`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Users mostly don't deal with profile fields directly, and should be able to assume that we've done our best to make them correct out of the box so that they don't have to worry about them, so I'd rather say:

Suggested change
If not specified, the default template specified on the model's profile will be used only if `native_output_requires_schema_in_instructions` on the profile is True.
If explicitly provided, it's used regardless of `native_output_requires_schema_in_instructions`.
If no template is specified but the model's profile indicates that it requires the schema to be sent as a prompt, the default template specified on the profile will be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some APIs that support constrained JSON schema output require JSON schema in instructions

2 participants