-
Notifications
You must be signed in to change notification settings - Fork 828
Erratic performance when using nested schemas #1659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
To me this looks like LLM-specific limitations or the due to the way the api provider sets up their prompt formatting. This is not stemming from pydantic-ai. |
@aus70 Do you know if the issue is in how PydanticAI formats the JSON schema for the
|
@DouweM The issue is correlated to the non-trivial JSON schema, but it's not caused directly by it, more precisely by the presence in the schema of a nested schema ($ref), because by hard-coding in a test call a JSON schema with references (see below an example related to a different set of classes) all models work fine. Obviously, I haven't been able to pinpoint the field that causes the problem.
|
@aus70 I ran your failing script locally, and the tools definition that's tripping up Qwen looks like this: [
{
"type": "function",
"function": {
"name": "final_result",
"description": "The final response which ends this conversation",
"parameters": {
"properties": {
"name": {
"description": "name",
"type": "string"
},
"count": {
"$ref": "#/$defs/Count"
}
},
"required": [
"name",
"count"
],
"type": "object",
"$defs": {
"Count": {
"properties": {
"count": {
"description": "count",
"type": "integer"
}
},
"required": [
"count"
],
"type": "object"
}
}
}
}
}
] It would likely be fine if Can you subclass from pydantic_ai.models import ModelRequestParameters
from pydantic_ai.models._json_schema import JsonSchema
from pydantic_ai.models.openai import _OpenAIJsonSchema, OpenAIModel
from pydantic_ai.settings import ModelSettings
from pydantic_ai.tools import ToolDefinition
class QwenModel(OpenAIModel):
def customize_request_parameters(self, model_request_parameters: ModelRequestParameters) -> ModelRequestParameters:
return _customize_request_parameters(model_request_parameters)
def _customize_request_parameters(model_request_parameters: ModelRequestParameters) -> ModelRequestParameters:
"""Customize the request parameters for OpenAI models."""
def _customize_tool_def(t: ToolDefinition):
schema_transformer = _QwenJsonSchema(t.parameters_json_schema, strict=t.strict)
parameters_json_schema = schema_transformer.walk()
if t.strict is None:
t = replace(t, strict=schema_transformer.is_strict_compatible)
return replace(t, parameters_json_schema=parameters_json_schema)
return ModelRequestParameters(
function_tools=[_customize_tool_def(tool) for tool in model_request_parameters.function_tools],
allow_text_output=model_request_parameters.allow_text_output,
output_tools=[_customize_tool_def(tool) for tool in model_request_parameters.output_tools],
)
class _QwenJsonSchema(_OpenAIJsonSchema):
def __init__(self, schema: JsonSchema, strict: bool | None):
super().__init__(schema, strict=strict)
self.prefer_inlined_defs = True This is all copied from |
|
Initial Checks
Description
Function call fails on some LLM when using nested classes as
output_type
It fails with:
Qwen/Qwen2.5-72B-Instruct-Turbo
on together.aiQwen/Qwen3-4B-fast
on nebius.comQwen/Qwen3-235B-A22B-fp8-tput
on together.aiIt succeeds with:
meta-llama/Llama-3.3-70B-Instruct-Turbo
on together.aiQwen3-4B-Q6_K.gguf
local on llama.cppTo replicate the bug, see the example code:
When using the classes
Count
,Size
,Name
asoutput_type
, all models succeed, when usingNameAndCount
some models fails. The error messages are the following:for togethe.ai models
pydantic_ai.exceptions.ModelHTTPError: status_code: 400, model_name: Qwen/Qwen2.5-72B-Instruct-Turbo, body: {'message': 'invalid tools grammar: Aborted(). Build with -sASSERTIONS for more info.', 'type': 'invalid_request_error', 'param': 'tools', 'code': None}
for the nebius.com model:
pydantic_ai.exceptions.ModelHTTPError: status_code: 400, model_name: Qwen/Qwen3-4B-fast, body: {'detail': "Invalid request. Please check the parameters and try again. Details: 1 validation error for list[function-wrap[__log_extra_fields__()]]\n Invalid JSON: EOF while parsing a value at line 1 column 0 [type=json_invalid, input_value='', input_type=str]
Possibly related to #1561 and #1414
Example Code
Python, Pydantic AI & LLM client version
The text was updated successfully, but these errors were encountered: