Skip to content

Commit 5085fd8

Browse files
split off responses API changes
1 parent a6152fe commit 5085fd8

2 files changed

Lines changed: 25 additions & 754 deletions

File tree

sentry_sdk/integrations/openai.py

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -306,80 +306,17 @@ def _set_responses_api_input_data(
306306
kwargs: "dict[str, Any]",
307307
integration: "OpenAIIntegration",
308308
) -> None:
309-
explicit_instructions: "Union[Optional[str], Omit]" = kwargs.get("instructions")
310-
messages: "Optional[Union[str, ResponseInputParam]]" = kwargs.get("input")
311-
312-
if not should_send_default_pii() or not integration.include_prompts:
313-
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "responses")
314-
_commmon_set_input_data(span, kwargs)
315-
return
309+
messages: "Optional[Union[ResponseInputParam, list[str]]]" = _get_input_messages(
310+
kwargs
311+
)
316312

317313
if (
318-
messages is None
319-
and explicit_instructions is not None
320-
and _is_given(explicit_instructions)
314+
messages is not None
315+
and len(messages) > 0
316+
and should_send_default_pii()
317+
and integration.include_prompts
321318
):
322-
set_data_normalized(
323-
span,
324-
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
325-
[
326-
{
327-
"type": "text",
328-
"content": explicit_instructions,
329-
}
330-
],
331-
unpack=False,
332-
)
333-
334-
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "responses")
335-
_commmon_set_input_data(span, kwargs)
336-
return
337-
338-
if messages is None:
339-
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "responses")
340-
_commmon_set_input_data(span, kwargs)
341-
return
342-
343-
instructions_text_parts: "list[TextPart]" = []
344-
if explicit_instructions is not None and _is_given(explicit_instructions):
345-
instructions_text_parts.append(
346-
{
347-
"type": "text",
348-
"content": explicit_instructions,
349-
}
350-
)
351-
352-
system_instructions = _get_system_instructions_responses(messages)
353-
# Deliberate use of function accepting completions API type because
354-
# of shared structure FOR THIS PURPOSE ONLY.
355-
instructions_text_parts += _transform_system_instructions(system_instructions)
356-
357-
if len(instructions_text_parts) > 0:
358-
set_data_normalized(
359-
span,
360-
SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS,
361-
instructions_text_parts,
362-
unpack=False,
363-
)
364-
365-
if isinstance(messages, str):
366-
normalized_messages = normalize_message_roles([messages]) # type: ignore
367-
scope = sentry_sdk.get_current_scope()
368-
messages_data = truncate_and_annotate_messages(normalized_messages, span, scope)
369-
if messages_data is not None:
370-
set_data_normalized(
371-
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, messages_data, unpack=False
372-
)
373-
374-
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "responses")
375-
_commmon_set_input_data(span, kwargs)
376-
return
377-
378-
non_system_messages = [
379-
message for message in messages if not _is_system_instruction_responses(message)
380-
]
381-
if len(non_system_messages) > 0:
382-
normalized_messages = normalize_message_roles(non_system_messages)
319+
normalized_messages = normalize_message_roles(messages) # type: ignore
383320
scope = sentry_sdk.get_current_scope()
384321
messages_data = truncate_and_annotate_messages(normalized_messages, span, scope)
385322
if messages_data is not None:

0 commit comments

Comments
 (0)