Skip to content

Models .settings attribute is not used in direct mode #2956

@moritzwilksch

Description

@moritzwilksch

Question

Setting

When setting a Model's .setting attribute during init, I would intuitively expect all generations created using this model to automatically use the provided setting unless overridden with model_settings in the model_request_sync call. However, this is not the case. Is this intended? The agent mode merges settings correctly:

merged_settings = merge_model_settings(model_used.settings, self.model_settings)
model_settings = merge_model_settings(merged_settings, model_settings)

Reproducer

from pydantic_ai.direct import model_request_sync
from pydantic_ai.models.openai import OpenAIResponsesModel, OpenAIResponsesModelSettings
from pydantic_ai.messages import ModelRequest, UserPromptPart, ModelMessage

# this is an invalid setting, we expect a 400
settings = OpenAIResponsesModelSettings(max_tokens=1)

model = OpenAIResponsesModel(
    model_name="gpt-4.1-mini",
    settings=settings,  # not honored when using model_request_sync
)

messages: list[ModelMessage] = [
    ModelRequest([UserPromptPart("Count to ten.")]),
]

response = model_request_sync(model=model, messages=messages)
print(response)
# ModelResponse(
#     parts=[
#         TextPart(
#             content="Sure! Here we go:\n\n1, 2, 3, 4, 5, 6, 7, 8, 9, 10.",
#             id="msg_68cd436de1b48194975ffe60262b19ee01cc9c17e50ec02e",
#         )
#     ],
#     usage=RequestUsage(
#         input_tokens=11, output_tokens=36, details={"reasoning_tokens": 0}
#     ),
#     model_name="gpt-4.1-mini-2025-04-14",
#     timestamp=datetime.datetime(2025, 9, 19, 11, 50, 5, tzinfo=TzInfo(UTC)),
#     provider_name="openai",
#     provider_details={"finish_reason": "completed"},
#     provider_response_id="resp_68cd436d1bfc8194a4f33c42f63301c801cc9c17e50ec02e",
#     finish_reason="stop",
# )

_ = model_request_sync(model=model, messages=messages, model_settings=settings)
# ModelHTTPError: status_code: 400, model_name: gpt-4.1-mini, body: {'message': "Invalid 'max_output_tokens': integer below minimum value. Expected a value >= 16, but got 1 instead.", 'type': 'invalid_request_error', 'param': 'max_output_tokens', 'code': 'integer_below_min_value'}

Additional Context

1.0.9

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions