|
3 | 3 | from sentry_sdk.integrations import DidNotEnable |
4 | 4 |
|
5 | 5 | from ..spans import ai_client_span, update_ai_client_span |
| 6 | +from sentry_sdk.consts import SPANDATA |
6 | 7 |
|
7 | 8 | from typing import TYPE_CHECKING |
8 | 9 |
|
@@ -33,13 +34,37 @@ def wrapped_get_model(cls, agent, run_config): |
33 | 34 | model = original_get_model(agent, run_config) |
34 | 35 | original_get_response = model.get_response |
35 | 36 |
|
| 37 | + # Wrap _fetch_response if it exists (for OpenAI models) to capture raw response model |
| 38 | + if hasattr(model, "_fetch_response"): |
| 39 | + original_fetch_response = model._fetch_response |
| 40 | + |
| 41 | + @wraps(original_fetch_response) |
| 42 | + async def wrapped_fetch_response(*args, **kwargs): |
| 43 | + # type: (*Any, **Any) -> Any |
| 44 | + response = await original_fetch_response(*args, **kwargs) |
| 45 | + if hasattr(response, "model"): |
| 46 | + agent._sentry_raw_response_model = str(response.model) |
| 47 | + return response |
| 48 | + |
| 49 | + model._fetch_response = wrapped_fetch_response |
| 50 | + |
36 | 51 | @wraps(original_get_response) |
37 | 52 | async def wrapped_get_response(*args, **kwargs): |
38 | 53 | # type: (*Any, **Any) -> Any |
39 | 54 | with ai_client_span(agent, kwargs) as span: |
40 | 55 | result = await original_get_response(*args, **kwargs) |
41 | 56 |
|
42 | | - update_ai_client_span(span, agent, kwargs, result) |
| 57 | + response_model = getattr(agent, "_sentry_raw_response_model", None) |
| 58 | + if response_model: |
| 59 | + agent_span = getattr(agent, "_sentry_agent_span", None) |
| 60 | + if agent_span: |
| 61 | + agent_span.set_data( |
| 62 | + SPANDATA.GEN_AI_RESPONSE_MODEL, response_model |
| 63 | + ) |
| 64 | + |
| 65 | + delattr(agent, "_sentry_raw_response_model") |
| 66 | + |
| 67 | + update_ai_client_span(span, agent, kwargs, result, response_model) |
43 | 68 |
|
44 | 69 | return result |
45 | 70 |
|
|
0 commit comments