fix: convert crawl_tool dict return to JSON string for type consistency (#636)

Keep fixing #631
This pull request updates the crawl_tool function to return its results as a JSON string instead of a dictionary, and adjusts the unit tests accordingly to handle the new return type. The changes ensure consistent serialization of output and proper validation in tests.
This commit is contained in:
Willem Jiang
2025-10-21 10:00:33 +08:00
committed by GitHub
parent e2ff765460
commit d30c4d00d3
2 changed files with 10 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates # Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
import json
import logging import logging
from typing import Annotated from typing import Annotated
@@ -22,7 +23,7 @@ def crawl_tool(
try: try:
crawler = Crawler() crawler = Crawler()
article = crawler.crawl(url) article = crawler.crawl(url)
return {"url": url, "crawled_content": article.to_markdown()[:1000]} return json.dumps({"url": url, "crawled_content": article.to_markdown()[:1000]})
except BaseException as e: except BaseException as e:
error_msg = f"Failed to crawl. Error: {repr(e)}" error_msg = f"Failed to crawl. Error: {repr(e)}"
logger.error(error_msg) logger.error(error_msg)

View File

@@ -1,3 +1,4 @@
import json
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from src.tools.crawl import crawl_tool from src.tools.crawl import crawl_tool
@@ -21,10 +22,11 @@ class TestCrawlTool:
result = crawl_tool(url) result = crawl_tool(url)
# Assert # Assert
assert isinstance(result, dict) assert isinstance(result, str)
assert result["url"] == url result_dict = json.loads(result)
assert "crawled_content" in result assert result_dict["url"] == url
assert len(result["crawled_content"]) <= 1000 assert "crawled_content" in result_dict
assert len(result_dict["crawled_content"]) <= 1000
mock_crawler_class.assert_called_once() mock_crawler_class.assert_called_once()
mock_crawler.crawl.assert_called_once_with(url) mock_crawler.crawl.assert_called_once_with(url)
mock_article.to_markdown.assert_called_once() mock_article.to_markdown.assert_called_once()
@@ -45,7 +47,8 @@ class TestCrawlTool:
result = crawl_tool(url) result = crawl_tool(url)
# Assert # Assert
assert result["crawled_content"] == short_content result_dict = json.loads(result)
assert result_dict["crawled_content"] == short_content
@patch("src.tools.crawl.Crawler") @patch("src.tools.crawl.Crawler")
@patch("src.tools.crawl.logger") @patch("src.tools.crawl.logger")