datarobot_genai.core.agents.history
history
Chat history extraction and normalization utilities.
This module provides helpers for extracting and summarizing prior chat
messages from RunAgentInput so that agent templates can inject
conversation history into their prompts.
NormalizedHistoryMessage
Bases: _NormalizedHistoryMessageRequired
Normalized representation of a single prior chat message.
This structure is intentionally minimal but preserves enough optional metadata for tool-heavy agents to reconstruct richer history when needed.
Required fields: - role: str - content: str
Optional fields (best-effort, may be absent): - tool_call_id: str | None - name: str | None - tool_calls: Any | None # e.g. OpenAI-style tool_calls payload
Source code in datarobot_genai/core/agents/history.py
extract_history_messages
extract_history_messages(run_agent_input: RunAgentInput, max_history: int) -> list[NormalizedHistoryMessage]
Return normalized prior messages to use as chat history.
Behaviour:
- Considers run_agent_input.messages in order.
- If the final message is a "user" message, treats everything before
it as history (so the latest user turn can be handled separately).
- Otherwise treats all provided messages as history.
- Converts messages into {role, content} dicts with string content.
- Truncates to the most recent max_history entries.
Special cases:
- When there are no messages, returns an empty list.
- When max_history <= 0, history is disabled and an empty list is returned.
This matches the documented semantics where 0 means "no history".
Source code in datarobot_genai/core/agents/history.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
build_history_summary_from_messages
Build a plain-text summary of prior turns for prompts.
This is a convenience helper around extract_history_messages that:
- Uses the same history selection semantics as extract_history_messages
- Truncates to the most recent max_history entries
- Normalizes them into {role, content} dicts
and then renders a newline-separated transcript of the form
role: content.
Returns an empty string when there is no history to include. Callers that embed the result in a prompt section (e.g. "Prior conversation:\n{summary}") should check for an empty return value and omit the section entirely to avoid dangling headers.
Source code in datarobot_genai/core/agents/history.py
drop_unpaired_boundary_tool_turns
drop_unpaired_boundary_tool_turns(history: list[NormalizedHistoryMessage]) -> list[NormalizedHistoryMessage]
Drop tool-call/result turns left unpaired at the history boundaries.
Two boundary cases, both of which most chat APIs reject; the structured (native-message) path drops them (the plain-text summary flattens and is unaffected, so it does not call this):
- Leading
toolresult with no preceding tool call — produced bymax_historytruncation, which keeps the most recent N turns (removing from the front) and so can slice a tool-call/result pair, leaving the result first. - Trailing
assistantmessage withtool_callsbut no followingtoolresult. This is not from truncation (truncation only trims the front): it is the supplied history simply ending on a tool-call turn (the current user turn is handled separately, so its result may not be in these messages).