|
| 1 | +"""Model-protocol compatibility helpers for agent runs.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import inspect |
| 6 | +from dataclasses import replace |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from agents import RunConfig |
| 10 | +from agents.run_config import CallModelData, ModelInputData |
| 11 | + |
| 12 | +_IMAGE_OUTPUT_ACK = "Image returned successfully; inspect the following user image." |
| 13 | +_IMAGE_INPUT_NOTE = "Images returned by the preceding tool call(s):" |
| 14 | + |
| 15 | + |
| 16 | +def adapt_image_tool_outputs_for_chat_completions( |
| 17 | + data: CallModelData[Any], |
| 18 | +) -> ModelInputData: |
| 19 | + """Move image tool outputs into a following user message. |
| 20 | +
|
| 21 | + Chat Completions accepts images in user content but restricts tool content |
| 22 | + to text. The Agents SDK uses Responses-style ``input_image`` parts for |
| 23 | + ``ToolOutputImage``, so rewrite those parts immediately before the model |
| 24 | + call while preserving a text result for every tool call. |
| 25 | + """ |
| 26 | + adapted: list[Any] = [] |
| 27 | + pending_images: list[dict[str, Any]] = [] |
| 28 | + |
| 29 | + def flush_images() -> None: |
| 30 | + if not pending_images: |
| 31 | + return |
| 32 | + adapted.append( |
| 33 | + { |
| 34 | + "role": "user", |
| 35 | + "content": [ |
| 36 | + {"type": "input_text", "text": _IMAGE_INPUT_NOTE}, |
| 37 | + *pending_images, |
| 38 | + ], |
| 39 | + } |
| 40 | + ) |
| 41 | + pending_images.clear() |
| 42 | + |
| 43 | + for item in data.model_data.input: |
| 44 | + if not (isinstance(item, dict) and item.get("type") == "function_call_output"): |
| 45 | + flush_images() |
| 46 | + adapted.append(item) |
| 47 | + continue |
| 48 | + |
| 49 | + output = item.get("output") |
| 50 | + if not isinstance(output, list): |
| 51 | + adapted.append(item) |
| 52 | + continue |
| 53 | + |
| 54 | + images = [ |
| 55 | + part for part in output if isinstance(part, dict) and part.get("type") == "input_image" |
| 56 | + ] |
| 57 | + if not images: |
| 58 | + adapted.append(item) |
| 59 | + continue |
| 60 | + |
| 61 | + text_parts = [ |
| 62 | + part for part in output if isinstance(part, dict) and part.get("type") == "input_text" |
| 63 | + ] |
| 64 | + adapted.append({**item, "output": text_parts or _IMAGE_OUTPUT_ACK}) |
| 65 | + pending_images.extend(images) |
| 66 | + |
| 67 | + flush_images() |
| 68 | + return ModelInputData(input=adapted, instructions=data.model_data.instructions) |
| 69 | + |
| 70 | + |
| 71 | +CHAT_COMPLETIONS_RUN_CONFIG = RunConfig( |
| 72 | + call_model_input_filter=adapt_image_tool_outputs_for_chat_completions |
| 73 | +) |
| 74 | + |
| 75 | + |
| 76 | +def with_chat_completions_compat(run_config: RunConfig | None) -> RunConfig: |
| 77 | + """Return a run config that preserves existing settings and adapts image outputs.""" |
| 78 | + if run_config is None: |
| 79 | + return CHAT_COMPLETIONS_RUN_CONFIG |
| 80 | + |
| 81 | + existing_filter = run_config.call_model_input_filter |
| 82 | + if existing_filter is None: |
| 83 | + return replace( |
| 84 | + run_config, |
| 85 | + call_model_input_filter=adapt_image_tool_outputs_for_chat_completions, |
| 86 | + ) |
| 87 | + if existing_filter is adapt_image_tool_outputs_for_chat_completions or getattr( |
| 88 | + existing_filter, "_openkb_chat_completions_compat", False |
| 89 | + ): |
| 90 | + return run_config |
| 91 | + |
| 92 | + async def composed_filter(data: CallModelData[Any]) -> ModelInputData: |
| 93 | + filtered = existing_filter(data) |
| 94 | + if inspect.isawaitable(filtered): |
| 95 | + filtered = await filtered |
| 96 | + return adapt_image_tool_outputs_for_chat_completions( |
| 97 | + CallModelData( |
| 98 | + model_data=filtered, |
| 99 | + agent=data.agent, |
| 100 | + context=data.context, |
| 101 | + ) |
| 102 | + ) |
| 103 | + |
| 104 | + setattr(composed_filter, "_openkb_chat_completions_compat", True) |
| 105 | + return replace(run_config, call_model_input_filter=composed_filter) |
0 commit comments