Skip to content

Build and run agents you can see, understand and trust.

fix(formatter): decode local file URI paths in Ollama formatters (#3107)

Long Jin705ab6150d7f
diff --git a/tests/formatter_ollama_test.py b/tests/formatter_ollama_test.py
index 9c1587bfb..90629be48 100644
--- a/tests/formatter_ollama_test.py
+++ b/tests/formatter_ollama_test.py
@@ -349,6 +349,48 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
multiagent_result,
)
+ async def test_formatters_preserve_local_file_uri_characters(self) -> None:
+ """Decode Unicode and literal percent escapes once, preserving '+'."""
+ with TemporaryDirectory() as directory:
+ for filename in ("caf\u00e9.png", "image%20.png", "image+1.png"):
+ path = Path(directory) / filename
+ path.write_bytes(base64.b64decode(self.image_b64))
+ # URI paths allow a literal '+'; form decoding would lose it.
+ url = path.as_uri().replace("%2B", "+")
+ source = URLSource(url=url, media_type="image/png")
+ self.assertEqual(str(source.url), url)
+ msgs = [
+ UserMsg(
+ name="user",
+ content=[DataBlock(source=source)],
+ ),
+ ]
+ for formatter, expected_content in (
+ (OllamaChatFormatter(), ""),
+ (
+ OllamaMultiAgentFormatter(),
+ "# Conversation History\n"
+ "The content between <history></history> tags "
+ "contains your conversation history\n"
+ "<history>\nuser:\n</history>",
+ ),
+ ):
+ with self.subTest(
+ filename=filename,
+ formatter=type(formatter).__name__,
+ ):
+ result = await formatter.format(msgs)
+ self.assertEqual(
+ result,
+ [
+ {
+ "role": "user",
+ "content": expected_content,
+ "images": [self.image_b64],
+ },
+ ],
+ )
+
async def test_chat_formatter_base64_image_in_tool_result(
self,
) -> None: