serve: content parts the server does not read (tool_reference, document, search_result, server tool blocks, file, audio) are left out as before, with a debug line, not a 400 (#1664)
Real clients send them next to text the model can answer from.
This commit is contained in:
1 parent
a6ae247561
commit
40bdd86d6d
2 files changed
+50
-50
No files matched your search
+9
-8
@@ -201,12 +201,13 @@ ANTHROPIC_BLOCKS = ("text",) + IMAGE_PARTS + ("thinking", "tool_use", "tool_resu
|
||||
|
||||
|
||||
def _check_parts(content, path: str, kinds):
|
||||
"""A request's content parts (on chat) or content blocks (on messages), checked while the request is converted:
|
||||
a part whose type this server does not read - a file, audio, a document - raises a ValueError naming the type
|
||||
and where it sits, which the server answers with a 400, as /v1/responses does; these two paths used to drop the
|
||||
part without a word, and the model answered about a file it had never seen. `kinds` is the types the place
|
||||
reads. A "refusal" part is read as its text, as /v1/responses does. Content that is not a list, and a part
|
||||
that is not an object, passes through: the readers skip it as they always did."""
|
||||
"""A request's content parts (on chat) or content blocks (on messages), looked at while the request is converted.
|
||||
A part whose type this server does not read (a file, audio, a document, Anthropic's tool_reference,
|
||||
search_result, server_tool_use / web_search_tool_result blocks, ...) is left out of the prompt exactly as it always
|
||||
was, with a debug line: real clients (Claude Code, Codex, opencode, Cline, Continue, Open WebUI) send such parts
|
||||
next to text the model can answer from, so refusing them would break requests that work. A "refusal" part is read
|
||||
as its text, as /v1/responses does. Content that is not a list, and a part that is not an object, passes through
|
||||
unchanged."""
|
||||
if not isinstance(content, list):
|
||||
return content
|
||||
out = content
|
||||
@@ -219,8 +220,8 @@ def _check_parts(content, path: str, kinds):
|
||||
out = list(content)
|
||||
out[j] = {"type": "text", "text": part.get("refusal") or ""}
|
||||
elif kind not in kinds:
|
||||
read = "text and images are" if set(kinds) & set(IMAGE_PARTS) else "text is"
|
||||
raise ValueError(f"{path}[{j}].type: content parts of type {kind!r} are not supported ({read})")
|
||||
LOGGER.debug("%s[%d].type: content part of type %r is not read by this server; left out of the prompt",
|
||||
path, j, kind)
|
||||
return out
|
||||
|
||||
|
||||
|
||||
+41
-42
@@ -86,55 +86,54 @@ class UnreadContentParts(unittest.TestCase):
|
||||
tok = ByteTokenizer()
|
||||
self.service = Service(MockEngine(tok, "ok"), tok, self.template)
|
||||
|
||||
def test_chat_file_and_audio_parts_are_refused(self):
|
||||
def test_chat_file_and_audio_parts_are_left_out(self):
|
||||
parts = ({"type": "file", "file": {"filename": "a.pdf", "file_data": "data:application/pdf;base64,AA"}},
|
||||
{"type": "input_audio", "input_audio": {"data": "AA", "format": "wav"}})
|
||||
plain = [{"role": "user", "content": "summarize"}, {"role": "user", "content": "this one"}]
|
||||
for part in parts:
|
||||
with self.subTest(kind=part["type"]):
|
||||
with self.assertRaises(ValueError) as raised:
|
||||
openai_to_messages({"messages": [
|
||||
{"role": "user", "content": "summarize the attached file"},
|
||||
{"role": "assistant", "content": "which file?"},
|
||||
{"role": "user", "content": [{"type": "text", "text": "this one"}, part]}]})
|
||||
self.assertEqual(str(raised.exception),
|
||||
f"messages[2].content[1].type: content parts of type {part['type']!r} are not "
|
||||
"supported (text and images are)")
|
||||
got = openai_to_messages({"messages": [
|
||||
{"role": "user", "content": "summarize"},
|
||||
{"role": "user", "content": [{"type": "text", "text": "this one"}, part]}]})
|
||||
want = openai_to_messages({"messages": [
|
||||
{"role": "user", "content": "summarize"},
|
||||
{"role": "user", "content": [{"type": "text", "text": "this one"}]}]})
|
||||
self.assertEqual(got, want)
|
||||
|
||||
def test_messages_document_block_is_refused(self):
|
||||
with self.assertRaises(ValueError) as raised:
|
||||
anthropic_to_messages({"messages": [
|
||||
{"role": "user", "content": "summarize the attached file"},
|
||||
{"role": "assistant", "content": "which file?"},
|
||||
def test_messages_blocks_real_clients_send_are_left_out(self):
|
||||
# tool_reference (Claude Code's tool search), documents, search results, server tool blocks, signed thinking
|
||||
extra = [{"type": "document", "source": {"type": "base64", "media_type": "application/pdf", "data": "AA"}},
|
||||
{"type": "search_result", "source": "s", "title": "t", "content": [{"type": "text", "text": "x"}]},
|
||||
{"type": "server_tool_use", "id": "s1", "name": "web_search", "input": {}},
|
||||
{"type": "web_search_tool_result", "tool_use_id": "s1", "content": []},
|
||||
{"type": "container_upload", "file_id": "f"}]
|
||||
for block in extra:
|
||||
with self.subTest(kind=block["type"]):
|
||||
got = anthropic_to_messages({"messages": [
|
||||
{"role": "user", "content": "hi"},
|
||||
{"role": "assistant", "content": [{"type": "text", "text": "ok"}, block]},
|
||||
{"role": "user", "content": [{"type": "text", "text": "go"}, block]}]})
|
||||
want = anthropic_to_messages({"messages": [
|
||||
{"role": "user", "content": "hi"},
|
||||
{"role": "assistant", "content": [{"type": "text", "text": "ok"}]},
|
||||
{"role": "user", "content": [{"type": "text", "text": "go"}]}]})
|
||||
self.assertEqual(got, want)
|
||||
|
||||
def test_messages_tool_reference_in_a_tool_result_is_left_out(self):
|
||||
def run(extra):
|
||||
return anthropic_to_messages({"messages": [
|
||||
{"role": "user", "content": "find a tool"},
|
||||
{"role": "assistant", "content": [{"type": "tool_use", "id": "t1", "name": "ToolSearch", "input": {}}]},
|
||||
{"role": "user", "content": [
|
||||
{"type": "text", "text": "this one"},
|
||||
{"type": "document", "source": {"type": "base64", "media_type": "application/pdf",
|
||||
"data": "AA"}}]}]})
|
||||
self.assertEqual(str(raised.exception),
|
||||
"messages[2].content[1].type: content parts of type 'document' are not supported "
|
||||
"(text and images are)")
|
||||
{"type": "tool_result", "tool_use_id": "t1", "content": [{"type": "text", "text": "found"}] + extra}]}]})
|
||||
self.assertEqual(run([{"type": "tool_reference", "tool_name": "mcp__x"}]), run([]))
|
||||
|
||||
def test_messages_document_in_a_tool_result_is_refused(self):
|
||||
with self.assertRaises(ValueError) as raised:
|
||||
anthropic_to_messages({"messages": [
|
||||
{"role": "user", "content": "read the pdf"},
|
||||
{"role": "assistant", "content": [{"type": "tool_use", "id": "t1", "name": "read", "input": {}}]},
|
||||
{"role": "user", "content": [
|
||||
{"type": "tool_result", "tool_use_id": "t1", "content": [
|
||||
{"type": "text", "text": "page 1"},
|
||||
{"type": "document", "source": {"type": "base64", "media_type": "application/pdf",
|
||||
"data": "AA"}}]}]}]})
|
||||
self.assertEqual(str(raised.exception),
|
||||
"messages[2].content[0].content[1].type: content parts of type 'document' are not "
|
||||
"supported (text and images are)")
|
||||
|
||||
def test_a_system_prompt_takes_text_only(self):
|
||||
with self.assertRaises(ValueError) as raised:
|
||||
openai_to_messages({"messages": [
|
||||
{"role": "system", "content": [{"type": "text", "text": "be brief"},
|
||||
{"type": "image_url", "image_url": "image.png"}]},
|
||||
{"role": "user", "content": "hi"}]})
|
||||
self.assertEqual(str(raised.exception),
|
||||
"messages[0].content[1].type: content parts of type 'image_url' are not supported (text is)")
|
||||
def test_a_system_prompt_with_an_image_part_is_read_as_text_as_before(self):
|
||||
got = openai_to_messages({"messages": [
|
||||
{"role": "system", "content": [{"type": "text", "text": "be brief"},
|
||||
{"type": "image_url", "image_url": "image.png"}]},
|
||||
{"role": "user", "content": "hi"}]})
|
||||
self.assertEqual(got[0][0]["content"], "be brief")
|
||||
|
||||
def test_messages_redacted_thinking_is_dropped(self):
|
||||
msgs = {"messages": [
|
||||
|
||||
Reference in new issue
Block a user