Skip to content

Conversation

moritzwilksch
Copy link
Contributor

@moritzwilksch moritzwilksch commented Sep 22, 2025

fixes #2956

by merging the Model.settings with the provided model_settings within each model's request method. In an earlier version I adjusted only the direct functions in direct.py but since users can use my_model.request(...) as well, the merging should be the responsibility of the Model.

@moritzwilksch
Copy link
Contributor Author

@DouweM Do you have any suggestions on how to test this?

@moritzwilksch moritzwilksch marked this pull request as ready for review September 23, 2025 11:51
@DouweM
Copy link
Collaborator

DouweM commented Sep 26, 2025

@moritzwilksch Might it be easier to do this in pydantic_ai.direct.model_request, analogous to how we do it in Agent.iter?

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

You could test it using FunctionModel like in this test:

def test_settings_merge_hierarchy():

@moritzwilksch
Copy link
Contributor Author

I implemented it there initially but users can also invoke Model.request and would expect the settings to be respected, right?

@DouweM
Copy link
Collaborator

DouweM commented Sep 29, 2025

@moritzwilksch Hmm, that's right, but then I think we should do the same thing for customize_request_parameters, which is not currently called by the models themselves either, just the model.request call sites in the agent graph and direct. I don't love putting those two calls in all the model request methods, though.

I'd prefer just one repeated line:

model_settings, model_request_parameters = self.prepare_request(model_settings, model_request_parameters)

Can you implement that on Model to call the merge_model_settings and customize_request_parameters methods and explain in the docstring that it can be overwritten (as the customize_request_parameters docstring says currently)? Then we can also remove the explicit customize_request_parameters calls.

@moritzwilksch
Copy link
Contributor Author

thanks for the pointers, changed. Let me know whether this is missing anything

@moritzwilksch moritzwilksch force-pushed the mw/model-settings-direct branch from ca4ed92 to 5b53535 Compare September 30, 2025 14:24
@moritzwilksch moritzwilksch force-pushed the mw/model-settings-direct branch from 5b53535 to 8c436af Compare September 30, 2025 14:26
@DouweM DouweM merged commit a426d55 into pydantic:main Oct 1, 2025
30 checks passed
@DouweM
Copy link
Collaborator

DouweM commented Oct 1, 2025

@moritzwilksch Thanks Moritz!

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.

Models .settings attribute is not used in direct mode
2 participants