Skip to content

Commit de13f6a

Browse files
authored
fix(openai): support acknowledged safety checks in computer use (#31984)
1 parent a5f1774 commit de13f6a

File tree

2 files changed

+78
-0
lines changed
  • libs/partners/openai

2 files changed

+78
-0
lines changed

libs/partners/openai/langchain_openai/chat_models/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3452,6 +3452,10 @@ def _make_computer_call_output_from_message(message: ToolMessage) -> dict:
34523452
# string, assume image_url
34533453
output = {"type": "input_image", "image_url": message.content}
34543454
computer_call_output["output"] = output
3455+
if "acknowledged_safety_checks" in message.additional_kwargs:
3456+
computer_call_output["acknowledged_safety_checks"] = message.additional_kwargs[
3457+
"acknowledged_safety_checks"
3458+
]
34553459
return computer_call_output
34563460

34573461

libs/partners/openai/tests/unit_tests/chat_models/test_base.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
_create_usage_metadata_responses,
6464
_format_message_content,
6565
_get_last_messages,
66+
_make_computer_call_output_from_message,
6667
_oai_structured_outputs_parser,
6768
)
6869

@@ -2454,3 +2455,76 @@ def test_get_request_payload_use_previous_response_id() -> None:
24542455
payload = llm._get_request_payload(messages)
24552456
assert "previous_response_id" not in payload
24562457
assert len(payload["input"]) == 1
2458+
2459+
2460+
def test_make_computer_call_output_from_message() -> None:
2461+
# List content
2462+
tool_message = ToolMessage(
2463+
content=[
2464+
{"type": "input_image", "image_url": "data:image/png;base64,<image_data>"}
2465+
],
2466+
tool_call_id="call_abc123",
2467+
additional_kwargs={"type": "computer_call_output"},
2468+
)
2469+
result = _make_computer_call_output_from_message(tool_message)
2470+
2471+
assert result == {
2472+
"type": "computer_call_output",
2473+
"call_id": "call_abc123",
2474+
"output": {
2475+
"type": "input_image",
2476+
"image_url": "data:image/png;base64,<image_data>",
2477+
},
2478+
}
2479+
2480+
# String content
2481+
tool_message = ToolMessage(
2482+
content="data:image/png;base64,<image_data>",
2483+
tool_call_id="call_abc123",
2484+
additional_kwargs={"type": "computer_call_output"},
2485+
)
2486+
result = _make_computer_call_output_from_message(tool_message)
2487+
2488+
assert result == {
2489+
"type": "computer_call_output",
2490+
"call_id": "call_abc123",
2491+
"output": {
2492+
"type": "input_image",
2493+
"image_url": "data:image/png;base64,<image_data>",
2494+
},
2495+
}
2496+
2497+
# Safety checks
2498+
tool_message = ToolMessage(
2499+
content=[
2500+
{"type": "input_image", "image_url": "data:image/png;base64,<image_data>"}
2501+
],
2502+
tool_call_id="call_abc123",
2503+
additional_kwargs={
2504+
"type": "computer_call_output",
2505+
"acknowledged_safety_checks": [
2506+
{
2507+
"id": "cu_sc_abc234",
2508+
"code": "malicious_instructions",
2509+
"message": "Malicious instructions detected.",
2510+
}
2511+
],
2512+
},
2513+
)
2514+
result = _make_computer_call_output_from_message(tool_message)
2515+
2516+
assert result == {
2517+
"type": "computer_call_output",
2518+
"call_id": "call_abc123",
2519+
"output": {
2520+
"type": "input_image",
2521+
"image_url": "data:image/png;base64,<image_data>",
2522+
},
2523+
"acknowledged_safety_checks": [
2524+
{
2525+
"id": "cu_sc_abc234",
2526+
"code": "malicious_instructions",
2527+
"message": "Malicious instructions detected.",
2528+
}
2529+
],
2530+
}

0 commit comments

Comments
 (0)