From 31f6ddef0484f9db2f7c68d7401faafb78e391f0 Mon Sep 17 00:00:00 2001 From: Gerson Azevedo Date: Sat, 14 Mar 2026 17:09:08 -0300 Subject: [PATCH] Fix Wikimedia image fetching by adding User-Agent header Wikimedia Commons returns 403 for requests without a User-Agent header, causing base64-encoded error pages instead of images. Add HEADERS constant and pass it to all httpx.get() calls that fetch remote images. Co-Authored-By: Claude Opus 4.6 --- anthropic_api_fundamentals/06_vision.ipynb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/anthropic_api_fundamentals/06_vision.ipynb b/anthropic_api_fundamentals/06_vision.ipynb index dc0f5018..a801ca3e 100644 --- a/anthropic_api_fundamentals/06_vision.ipynb +++ b/anthropic_api_fundamentals/06_vision.ipynb @@ -965,9 +965,11 @@ "import base64\n", "import httpx\n", "\n", + "HEADERS = {\"User-Agent\": \"AnthropicCoursesBot/1.0 (https://github.com/anthropics/courses)\"}\n", + "\n", "image_url = \"https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Church_of_light.jpg/1599px-Church_of_light.jpg\"\n", "image_media_type = \"image/jpeg\"\n", - "image_data = base64.b64encode(httpx.get(image_url).content).decode(\"utf-8\")\n", + "image_data = base64.b64encode(httpx.get(image_url, headers=HEADERS).content).decode(\"utf-8\")\n", "\n", "messages=[\n", " {\n", @@ -1034,7 +1036,7 @@ "\n", "def get_image_dict_from_url(image_url):\n", " # Send a GET request to the image URL and retrieve the content\n", - " response = httpx.get(image_url)\n", + " response = httpx.get(image_url, headers=HEADERS)\n", " image_content = response.content\n", "\n", " # Determine the media type of the image based on the URL extension\n",