This section dynamically generates system prompts based on the context at run time, allowing the same Agent to respond in different languages to different User users.
System prompt is not necessarily a fixed string. It can be dynamically generated based on user language, role, permissions, region or business context. In this lesson, UseUse user language will be used as a minimal example.
| input context | Generated system prompt |
|---|---|
English | You are a helpful assistant. |
Irish | You are a helpful assistant. only respond in Irish. |
Spanish | You are a helpful assistant. only respond in Spanish. |
Run-time context Use dataclass expression. When calling Use Agent, pass different user_language, and the middleware can generate different prompts.
from dataclasses import dataclass
from langchain.agents.middleware import dynamic_prompt, ModelRequest
@dataclass
class LanguageContext:
user_language: str = "English"Module 2 has learned about runtime context; in this section, Put context Use is generated at the prompt instead of being read by the tool.
The @dynamic_prompt function receives ModelRequest and can access request.runtime.context. The return value will become the system prompt for this model adjustment.
@dynamic_prompt
def user_language_prompt(request: ModelRequest) -> str:
"""Generate system prompt based on user role."""
user_language = request.runtime.context.user_language
base_prompt = "You are a helpful assistant."
if user_language != "English":
return f"{base_prompt} only respond in {user_language}."
elif user_language == "English":
return base_promptThis middleware does not directly answer the Use user, but generates system messages for the model to call Use. The real answer still lies with the model.
Declare context_schema and middleware when creating Agent. Each subsequent invoke can pass a different context.
agent = create_agent(
model=deepseek_model(),
context_schema=LanguageContext,
middleware=[user_language_prompt],
)Don't Use to copy one Agent for each language. When an Agent adds a dynamic prompt, it can switch behaviors according to context.
Notebook inputs Irish, Spanish, and French respectively. The questions are the same, but the system prompts are different, so the answer language will also change.
response = agent.invoke(
{"messages": [HumanMessage(content="Hello, how are you?")]},
context=LanguageContext(user_language="Irish"),
)
print(response["messages"][-1].content)
response = agent.invoke(
{"messages": [HumanMessage(content="Hello, how are you?")]},
context=LanguageContext(user_language="Spanish"),
)
print(response["messages"][-1].content)
response = agent.invoke(
{"messages": [HumanMessage(content="Hello, how are you?")]},
context=LanguageContext(user_language="French"),
)
print(response["messages"][-1].content)Language is just an example. The same model can generate different rules according to user roles, different terms according to product lines, and different compliance prompts according to regions.
dynamic_promptUse to generate system prompt when running.context_schemaUse in combination.