xml_tool_message
langroid/agent/xml_tool_message.py
XMLToolMessage
¶
Bases: ToolMessage
Abstract class for tools formatted using XML instead of JSON.
When a subclass defines a field with the attribute verbatim=True,
instructions are sent to the LLM to ensure the field's content is:
- preserved as is, including whitespace, indents, quotes, newlines, etc
with no escaping, and
- enclosed in a CDATA section in the XML output.
This is useful for LLMs sending code as part of a tool;
results can be far superior compared to sending code in JSON-formatted tools,
where code needs to confirm to JSON's strict rules and escaping requirements.
(see test_xml_tool_message.py for an example).
extract_field_values(formatted_string)
classmethod
¶
Extracts field values from an XML-formatted string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formatted_string
|
str
|
The XML-formatted string to parse. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Optional[Dict[str, Any]]: A dictionary containing the extracted field values, where keys are the XML element names and values are their corresponding contents. |
Optional[Dict[str, Any]]
|
Returns None if parsing fails or the root element is not a dictionary. |
Raises:
| Type | Description |
|---|---|
XMLSyntaxError
|
If the input string is not valid XML. |
Source code in langroid/agent/xml_tool_message.py
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
parse(formatted_string)
classmethod
¶
Parses the XML-formatted string and returns an instance of the class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formatted_string
|
str
|
The XML-formatted string to parse. |
required |
Returns:
| Type | Description |
|---|---|
Optional[XMLToolMessage]
|
Optional["XMLToolMessage"]: An instance of the class if parsing succeeds, None otherwise. |
Source code in langroid/agent/xml_tool_message.py
format_example()
¶
Format the current instance as an XML example.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A string representation of the current instance in XML format. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the result from etree.tostring is not a string. |
Source code in langroid/agent/xml_tool_message.py
find_candidates(text)
classmethod
¶
Finds XML-like tool message candidates in text, with relaxed opening tag rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text to search for XML structures. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List of XML strings. For fragments missing the root opening tag but having |
List[str]
|
valid XML structure and root closing tag, prepends the root opening tag. |
Example
With root_tag="tool", given:
"Hello