What is the proper way to handle image returns from an external tool?
Context
The code processes tool results that can include text or images in base64 form.
After calling a tool, the code constructs new chat messages containing:
A role set to "tool",
A content key that holds a list of blocks, such as {"type": "text", "text": "..."} or {"type": "image_url", ...}.
These messages are then appended to the conversation history and passed through Pydantic validation.
Example Excerpt
python
Copy code
tools_content = [
{"type": "text", "text": part.text}
for part in filter(lambda x: x.type in ["text", "image"], tool_call_result.content)
]
if not tools_content:
tools_content = [
{"type": "text", "text": "the tool call result is empty"}
]
request.messages.append(
ChatCompletionRequestMessage.model_validate(
{
"role": "tool", # Custom role
"content": tools_content, # List of blocks
"tool_call_id": tool_call.id,
}
)
)
Observed Behavior
When ChatCompletionRequestMessage.model_validate(...) runs on a message that includes "role": "tool" and "content" with structures like {"type": "image_url"}, Pydantic reports multiple validation errors. Examples of these errors include:
typescript
Copy code
Input should be a valid string [type=string_type]
...
Input should be 'text' [type=enum, input_value='image_url']
...
Input should be one of 'system', 'user', 'assistant', 'function' [type=enum, input_value='tool']
These errors occur because the Pydantic schema is not accepting custom roles beyond the standard ones ("system", "user", "assistant", "function") or complex content formats such as {"type": "image_url", ...} blocks.