Skip to content

fix(ai): web_fetch decodes pages by their BOM and declared charset #895

Closed as not planned

Description

@dan-petty

Context & Rationale

#860 split the web_fetch hardening entry (docs/ROADMAP.md, at 308a71f line 878) into four PRs and delivered PR1, the per-hop egress veto, as a v0.2.24 critical fix. This is PR3. resp.text skips the WHATWG BOM and <meta charset> steps, so pages arrive as mojibake.

Scope & Deliverables

  • Deliverable: a default_encoding callable (httpx2/_models.py:539,672-680) prescans 1024 bytes for <meta charset>, then charset_normalizer, pinned in pyproject.toml; check resp.content for a BOM and set resp.encoding first. Port vibes test_crawler.py:306-351,396-475 onto httpx2.MockTransport.
  • Acceptance criteria: every behaviour named above has a test that fails before the change; tests use httpx2.MockTransport and never touch the network; uv run devops ci passes.
  • Constraint: the per-hop veto from fix(ai): web_fetch vetoes every redirect hop, reports failures as failures and renders pages as untrusted text #860 stays the only egress check, and a failure is never returned as page text.

Strategic Alignment

Proposed design

Problem statement

The web_fetch function in the AI module does not properly decode web pages by their BOM (Byte Order Mark) or declared charset, resulting in mojibake (garbled text) when pages are fetched. This issue affects the accuracy and usability of fetched content.

Acceptance criteria

  • Criterion 1: The default_encoding callable correctly prescans 1024 bytes for `` declaration.
    Verification: A test using httpx2.MockTransport verifies that the function identifies the charset from HTML meta tags within the first 1024 bytes.
  • Criterion 2: The default_encoding callable uses charset_normalizer to determine encoding when no meta charset is found.
    Verification: A test using httpx2.MockTransport verifies that the function correctly identifies encoding using charset_normalizer on content without explicit charset.
  • Criterion 3: The default_encoding callable checks for BOM in resp.content and sets resp.encoding accordingly.
    Verification: A test using httpx2.MockTransport verifies that the function correctly detects and applies BOM-based encoding from response content.
  • Criterion 4: All behaviors named above have tests that fail before the change and pass after the change.
    Verification: Running uv run devops ci passes with the new implementation, confirming all tests are working correctly.

Key questions

  • What tools are used for charset detection?: The charset_normalizer library is used for charset detection, and it is pinned in pyproject.toml. [sources: pyproject.toml, uv.lock:471]

Open questions

  • What is the purpose of the default_encoding callable in the context of web_fetch? (missing verified source)
  • What is the expected behavior of resp.text in relation to BOM and `` steps? (missing verified source)
  • What tests are required to validate the fix? (missing verified source)
  • What constraint is imposed on this fix? (missing verified source)

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/bug
    scope/ai
    priority/p2-medium
    on Oct 1, 2026
  2. added a commit that references this issue on Oct 1, 2026
  3. added this to the milestone on Oct 20, 2026
  4. dan-petty commented on Oct 20, 2026

    @dan-petty
    OwnerAuthor

    Status set back to New at 2954109: refine found it not ready (see Key questions, Fit and Suspected block in the proposed design in the issue body).

  5. removed this from the milestone on Oct 23, 2026
  6. dan-petty commented on Oct 23, 2026

    @dan-petty
    OwnerAuthor

    Not planned: #1854 deleted the web_fetch tool, and its task file says this item no longer applies. A later fetch feature can use pydantic_ai.common_tools.web_fetch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p2-mediumscope/aitype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions