datawhalechina/hello-agents · warning · HTTPException

获取景点图片失败: {str(e)}

Error message

获取景点图片失败: {str(e)}

What it means

HTTPException(500) raised by GET /poi/photo when fetching an attraction photo (Unsplash-based) fails. The route fetches a photo_url for a scenic-spot name; failures include HTTP errors from the image provider, missing API key for Unsplash, or exceptions while resolving the name to a photo.

Source

Thrown at code/chapter13/helloagents-trip-planner/backend/app/api/routes/poi.py:125

        # 搜索景点图片
        photo_url = unsplash_service.get_photo_url(f"{name} China landmark")

        if not photo_url:
            # 如果没找到,尝试只用景点名称搜索
            photo_url = unsplash_service.get_photo_url(name)

        return {
            "success": True,
            "message": "获取图片成功",
            "data": {
                "name": name,
                "photo_url": photo_url
            }
        }

    except Exception as e:
        print(f"❌ 获取景点图片失败: {str(e)}")
        raise HTTPException(
            status_code=500,
            detail=f"获取景点图片失败: {str(e)}"
        )

View on GitHub (pinned to 606a07d341)

Solutions

  1. Check the server log '❌ 获取景点图片失败' line for the provider error
  2. Set the Unsplash API key in backend/.env if the service requires one
  3. Reduce request frequency or cache photo URLs per attraction name to stay under rate limits
  4. Degrade gracefully on the frontend: fall back to a bundled placeholder when this endpoint 500s

Example fix

# before
photo = requests.get(f'{BASE}/poi/photo', params={'name': name}).json()['data']['photo_url']

# after
r = requests.get(f'{BASE}/poi/photo', params={'name': name})
photo = r.json()['data']['photo_url'] if r.ok else PLACEHOLDER_IMG
# cache `photo` by name to avoid repeat provider calls
Defensive patterns

Strategy: fallback

Validate before calling

_photo_cache = {}

def get_photo(name: str) -> str:
    if name in _photo_cache:
        return _photo_cache[name]
    r = requests.get(f'{BASE}/poi/photo', params={'name': name}, timeout=10)
    url = r.json()['data']['photo_url'] if r.ok else PLACEHOLDER
    _photo_cache[name] = url
    return url

Try / catch

try:
    photo_url = fetch_attraction_photo(name)
except Exception:
    photo_url = '/static/placeholder.jpg'  # photos are cosmetic; never fail the page

Prevention

When it happens

Trigger: UNSLASH/UNSPLASH_ACCESS_KEY (or equivalent) unset so the image lookup throws; Unsplash API rate limit hit; network egress blocked; attraction name that the service cannot map to any photo raising inside the helper.

Common situations: Deploying without the image-provider key in .env; hitting the 50-req/hour Unsplash demo limit during demos/tests; sandboxed environments with no outbound internet.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/ea4ca348c471db94. Report an issue: GitHub.