patcher
agent_cover.instrumentation.llm_providers.patcher
Module for instrumenting LLM (Large Language Model) providers.
This module provides the infrastructure to intercept method calls to various LLM libraries (like OpenAI), extract the generated content, and pass it to an analysis engine. It uses a patching strategy to wrap target methods dynamically.
Attributes
Classes
LLMProviderInstrumentor
Bases: BaseInstrumentor
Instrumentor that sits at the edge of the system (External APIs).
Unlike other instrumentors that track code coverage, this tracks Data Coverage. It intercepts the raw string response from the LLM to analyze if business logic requirements (Decisions) were met.
Attributes:
| Name | Type | Description |
|---|---|---|
registry |
Optional[AgentRegistry]
|
The registry for agent components. |
analyzer |
OutputAnalyzer
|
The component responsible for analyzing extracted text. |
importer_func |
Callable
|
Function used to import modules dynamically. |
module_iterator |
Callable
|
Function that returns the current mapping of loaded modules. |
targets_provider |
Callable
|
Function that returns a list of targets to instrument. |
extractors |
List[PayloadExtractor]
|
List of strategies used to parse LLM results. |
is_instrumented |
bool
|
Flag indicating if instrumentation has already run. |
Methods:
| Name | Description |
|---|---|
instrument |
Performs the actual patching of target methods. |
_resolve_target |
helper to find the specific object/method to patch. |
_create_wrapper |
Creates the closure that wraps the original method. |
_delegate_extraction |
Iterates through extractors to parse the result. |
Source code in src/agent_cover/instrumentation/llm_providers/patcher.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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 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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
Functions
__init__(registry=None, analyzer=None, patch_manager=None, importer_func=None, module_iterator=None, targets_provider=None, extractors=None)
Initializes the LLMProviderInstrumentor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
The registry instance. |
None
|
analyzer
|
Optional[OutputAnalyzer]
|
Custom analyzer instance. |
None
|
patch_manager
|
Optional[PatchManager]
|
The patch manager for safe patching. |
None
|
importer_func
|
Optional[Callable]
|
Custom import function. |
None
|
module_iterator
|
Optional[Callable]
|
Custom module iterator provider (for dependency injection). |
None
|
targets_provider
|
Optional[Callable]
|
Custom provider for patch targets. |
None
|
extractors
|
Optional[List[PayloadExtractor]]
|
Custom list of payload extractors. |
None
|
Source code in src/agent_cover/instrumentation/llm_providers/patcher.py
instrument()
Applies patches to the defined LLM provider targets.
This method iterates through the targets provided by targets_provider.
It loads the necessary modules (using a snapshot to allow for test isolation)
and wraps the specified methods/functions to enable output analysis.
Source code in src/agent_cover/instrumentation/llm_providers/patcher.py
OpenAIExtractor
Bases: PayloadExtractor
Extractor implementation for OpenAI-style response objects.
This class handles standard OpenAI response dictionaries or objects, attempting to retrieve content from 'choices', 'messages', or legacy 'text' fields.
Methods:
| Name | Description |
|---|---|
extract |
Extracts content from OpenAI response structures. |
Source code in src/agent_cover/instrumentation/llm_providers/patcher.py
Functions
extract(result)
Extracts text from an OpenAI response object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Any
|
The OpenAI response object (or dict-like object). |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: The content string if found, otherwise None. |
Source code in src/agent_cover/instrumentation/llm_providers/patcher.py
PayloadExtractor
Interface for extracting text content from specific LLM result objects.
If you want to add support for a new LLM provider (e.g., Anthropic, VertexAI),
you should subclass this and implement the extract method.
Methods:
| Name | Description |
|---|---|
extract |
Should return the plain text string of the LLM's response. |
Source code in src/agent_cover/instrumentation/llm_providers/patcher.py
Functions
extract(result)
Extracts the text payload from a result object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Any
|
The return value from the intercepted LLM call (e.g., an OpenAI object). |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: The extracted text string. Returns |
Optional[str]
|
cannot handle this specific result object. |
Source code in src/agent_cover/instrumentation/llm_providers/patcher.py
StringExtractor
Bases: PayloadExtractor
Extractor implementation for simple string results.
This class handles cases where the LLM function returns a plain string directly.
Methods:
| Name | Description |
|---|---|
extract |
Returns the result itself if it is a string. |
Source code in src/agent_cover/instrumentation/llm_providers/patcher.py
Functions
extract(result)
Validates and returns the result if it is a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Any
|
The return value to check. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: The result string if valid, otherwise None. |
Source code in src/agent_cover/instrumentation/llm_providers/patcher.py
Functions
instrument_llm_providers()
Convenience function to instantiate and run the instrumentor.
Returns:
| Name | Type | Description |
|---|---|---|
LLMProviderInstrumentor |
LLMProviderInstrumentor
|
The initialized and executed instrumentor instance. |