ModelRequest Class — langchain Architecture
Architecture documentation for the ModelRequest class in types.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD f413466f_6fee_56c2_0df7_03cfa4e2b92a["ModelRequest"] 11af10c7_4150_b78c_f01b_9e5dfc840a42["types.py"] f413466f_6fee_56c2_0df7_03cfa4e2b92a -->|defined in| 11af10c7_4150_b78c_f01b_9e5dfc840a42 6f8d66ee_b52c_0cb1_1866_d1aa0cbeb5ac["__init__()"] f413466f_6fee_56c2_0df7_03cfa4e2b92a -->|method| 6f8d66ee_b52c_0cb1_1866_d1aa0cbeb5ac 4f3671c6_2e38_d6b4_0343_aba7c976576a["system_prompt()"] f413466f_6fee_56c2_0df7_03cfa4e2b92a -->|method| 4f3671c6_2e38_d6b4_0343_aba7c976576a 908b36dd_dc6d_a5b3_4e91_fb69f2583c51["__setattr__()"] f413466f_6fee_56c2_0df7_03cfa4e2b92a -->|method| 908b36dd_dc6d_a5b3_4e91_fb69f2583c51 e2585731_77e3_b7bd_6c0a_772b884fe839["override()"] f413466f_6fee_56c2_0df7_03cfa4e2b92a -->|method| e2585731_77e3_b7bd_6c0a_772b884fe839
Relationship Graph
Source Code
libs/langchain_v1/langchain/agents/middleware/types.py lines 89–268
class ModelRequest(Generic[ContextT]):
"""Model request information for the agent.
Type Parameters:
ContextT: The type of the runtime context. Defaults to `None` if not specified.
"""
model: BaseChatModel
messages: list[AnyMessage] # excluding system message
system_message: SystemMessage | None
tool_choice: Any | None
tools: list[BaseTool | dict[str, Any]]
response_format: ResponseFormat[Any] | None
state: AgentState[Any]
runtime: Runtime[ContextT]
model_settings: dict[str, Any] = field(default_factory=dict)
def __init__(
self,
*,
model: BaseChatModel,
messages: list[AnyMessage],
system_message: SystemMessage | None = None,
system_prompt: str | None = None,
tool_choice: Any | None = None,
tools: list[BaseTool | dict[str, Any]] | None = None,
response_format: ResponseFormat[Any] | None = None,
state: AgentState[Any] | None = None,
runtime: Runtime[ContextT] | None = None,
model_settings: dict[str, Any] | None = None,
) -> None:
"""Initialize ModelRequest with backward compatibility for system_prompt.
Args:
model: The chat model to use.
messages: List of messages (excluding system prompt).
tool_choice: Tool choice configuration.
tools: List of available tools.
response_format: Response format specification.
state: Agent state.
runtime: Runtime context.
model_settings: Additional model settings.
system_message: System message instance (preferred).
system_prompt: System prompt string (deprecated, converted to SystemMessage).
Raises:
ValueError: If both `system_prompt` and `system_message` are provided.
"""
# Handle system_prompt/system_message conversion and validation
if system_prompt is not None and system_message is not None:
msg = "Cannot specify both system_prompt and system_message"
raise ValueError(msg)
if system_prompt is not None:
system_message = SystemMessage(content=system_prompt)
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecationWarning)
self.model = model
self.messages = messages
self.system_message = system_message
self.tool_choice = tool_choice
self.tools = tools if tools is not None else []
self.response_format = response_format
self.state = state if state is not None else {"messages": []}
self.runtime = runtime # type: ignore[assignment]
self.model_settings = model_settings if model_settings is not None else {}
@property
def system_prompt(self) -> str | None:
"""Get system prompt text from system_message.
Returns:
The content of the system message if present, otherwise `None`.
"""
if self.system_message is None:
return None
return self.system_message.text
def __setattr__(self, name: str, value: Any) -> None:
"""Set an attribute with a deprecation warning.
Source
Frequently Asked Questions
What is the ModelRequest class?
ModelRequest is a class in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/types.py.
Where is ModelRequest defined?
ModelRequest is defined in libs/langchain_v1/langchain/agents/middleware/types.py at line 89.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free