Skip to content

Conversation

@fang-tech
Copy link

summary

This PR implements ConditionalModelSettingsMiddleware, a middleware for dynamically configuring model bind settings based on runtime conditions.

What it does

Provides a fluent API (.when(condition).use(settings)) to apply different model_settings (e.g., parallel_tool_calls, strict) based on runtime conditions like message count or state flags. Multiple matching conditions apply cumulatively, with later settings overriding earlier ones for the same keys.

Use cases

  • Disable parallel tool calls for long conversations
  • Enable strict mode based on state flags
  • Dynamically adjust tool calling behavior based on context
  • Fine-grained control over model behavior in different scenarios

Why this PR

ref #34031 and #34010. Adding model_settings directly to create_agent would cause problems:

  • User confusion: Unclear which settings belong in model initialization vs. model_settings
  • Limited scope: Only agent-level configuration; users with scenario-specific needs would still need custom middleware
  • Unnecessary complexity: Having both create_agent and middleware modify model_settings simultaneously

Solution advantages

  • Flexible configuration: Supports both agent-level and condition-based local configurations
  • Clear timing: model_settings application happens explicitly in wrap_model_call hooks
  • Easy migration: Existing middleware users can easily adopt this built-in solution
  • Separation of concerns: Keeps create_agent simple while encapsulating dynamic logic in dedicated middleware

Issue: Fixes #34031

@github-actions github-actions bot added langchain Related to the package `langchain` feature labels Nov 20, 2025
@fang-tech fang-tech closed this Nov 20, 2025
@fang-tech fang-tech reopened this Nov 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature langchain Related to the package `langchain`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider model_settings on create_agent

2 participants