Skip to content

The agent engineering platform.

core[patch]: Enhance link extraction with query parameters (#20259)

YH2aca7fcdcfce
diff --git a/libs/core/langchain_core/utils/html.py b/libs/core/langchain_core/utils/html.py
index 837b19ed10..3e41c187b4 100644
--- a/libs/core/langchain_core/utils/html.py
+++ b/libs/core/langchain_core/utils/html.py
@@ -88,6 +88,8 @@ def extract_sub_links(
absolute_path = f"{parsed_url.scheme}:{link}"
else:
absolute_path = urljoin(url, parsed_link.path)
+ if parsed_link.query:
+ absolute_path += f"?{parsed_link.query}"
absolute_paths.add(absolute_path)
except Exception as e:
if continue_on_failure:
diff --git a/libs/core/tests/unit_tests/utils/test_html.py b/libs/core/tests/unit_tests/utils/test_html.py
index a2c80f6e65..a6332e4b60 100644
--- a/libs/core/tests/unit_tests/utils/test_html.py
+++ b/libs/core/tests/unit_tests/utils/test_html.py
@@ -183,3 +183,27 @@ def test_prevent_outside() -> None:
)
)
assert actual == expected
+
+
+def test_extract_sub_links_with_query() -> None:
+ html = (
+ '<a href="https://foobar.com?query=123">one</a>'
+ '<a href="/hello?query=456">two</a>'
+ '<a href="//foobar.com/how/are/you?query=789">three</a>'
+ '<a href="doing?query=101112"></a>'
+ )
+
+ expected = sorted(
+ [
+ "https://foobar.com?query=123",
+ "https://foobar.com/hello?query=456",
+ "https://foobar.com/how/are/you?query=789",
+ "https://foobar.com/hello/doing?query=101112",
+ ]
+ )
+ actual = sorted(
+ extract_sub_links(
+ html, "https://foobar.com/hello/bill.html", base_url="https://foobar.com"
+ )
+ )
+ assert actual == expected, f"Expected {expected}, but got {actual}"