Model Callback for Red Teaming
The model callback in deepeval can be a string or an instance of DeepEvalBaseLLM or simply a callback function that wraps around your target LLM system that you are red teaming. However, it is essential that you define this correctly because deepeval will be calling your model callback at red teaming time to attack your LLM system with the adversarial inputs it has generated.
For passing a string or an instance of DeepEvalBaseLLM, you can pass the model callback as shown below:
...
red_team(
attacks=[...],
vulnerabilities=[...],
model_callback="openai/gpt-5.2",
)For strings, you can pass a model with its provider prefix separated by a /. Ex: google/gemini-2.5-flash. The available providers that deepeval supports are: openai, anthropic, google, xai, moonshotai, deepseek, ollama.
from deepeval.models import AzureOpenAIModel
model = AzureOpenAIModel(...)
red_team(
attacks=[...],
vulnerabilities=[...],
model_callback=model,
)deepeval allows you to even pass your custom models which inherit from DeepEvalBaseLLM and implements the required methods as described in the custom LLM documentation.
For custom callback functions, here's how you can define your model callback:
from deepeval.red_team.test_case import RTTurn, ToolCall
async def model_callback(input: str, turns: list[RTTurn] = None) -> str:
# Replace this with your actual LLM application
# This could be a RAG pipeline, chatbot, agent, etc.
return RTTurn(
role="assistant",
content="Your agent's response here...",
retrieval_context=[
"Your retieval context here",
"..."
],
tools_called=[
ToolCall(name="SearchDatabase")
]
)When defining your model callback function, there are TWO hard rules you MUST follow:
- The function signature must have TWO parameters, a mandatory first parameter that accepts a
stringand an optional parameter that accepts a list ofRTTurnobjects. Please default the second parameter toNone. - The function must only return an
RTTurnobject (recommended) withtools_calledandretrieval_context(if applicable) or just a simple string which is the response of the target model.- The
retrieval_contextcan be a list of strings that represents the context retrieved by your retriever - The
tools_calledmust be a list ofToolCallobjects, the sameToolCallfromdeepeval.test_caseyou use in LLM test cases.
- The
You can also make your model callback asynchronous if you want to speed up red teaming, but it is not a hard requirement.