Skip to content

fix(tianditu): add request timeouts and credential guards#3469

Open
Harsh23Kashyap wants to merge 2 commits into
langgenius:mainfrom
Harsh23Kashyap:fix/tianditu-requests-timeouts-and-credential-guards
Open

fix(tianditu): add request timeouts and credential guards#3469
Harsh23Kashyap wants to merge 2 commits into
langgenius:mainfrom
Harsh23Kashyap:fix/tianditu-requests-timeouts-and-credential-guards

Conversation

@Harsh23Kashyap

Copy link
Copy Markdown
Contributor

Summary

Mirrors the same three-bug reliability pattern that PR #3456 (already MERGED on 2026-07-20, tools/google) and PR #3466 (tools/searchapi) / PR #3468 (tools/serper) just fixed, applied to tools/tianditu.

Refs #3465 (umbrella tracker).

Changes

  • Every requests.get(...) call across the three tool files (geocoder.py, staticmap.py, poisearch.py) now passes timeout=10. 10 s is well above the upstream Tianditu API p99 and well below the framework-level 120 s ceiling.
  • self.runtime.credentials["tianditu_api_key"] is replaced with an early guard that yields "Tianditu API key is required." and returns; the value is read via .get(). No more KeyError on a missing or empty key.
  • Wraps all HTTP calls in try / except requests.exceptions.RequestException so a timeout, connection reset, DNS failure, or 5xx surfaces as a friendly message instead of a stack trace.
  • New tools/tianditu/tests/test_tianditu.py with 11 in-process test cases (no pytest or dify_plugin runtime needed — uses only unittest.mock + minimal sys-modules stubs).
  • manifest.yaml PATCH bump 0.0.7 -> 0.0.8.

tianditu does not take user-supplied hl / gl (the geocoder API takes a keyWord and region from the request body, not as user params), so the third bug from PR #3456 (invoke fall-through on invalid hl / gl) does not apply.

Change Type

  • Documentation / non-plugin change
  • Non-LLM plugin only (LLM and multimodal model plugin)

LLM Plugin Checklist

Not applicable — this is a tool plugin.

Version

  • manifest.yaml is 0.0.8.
  • requests>=2.34.2 is already declared and locked.

Testing

  • python3 -m pytest tests/test_tianditu.py -v -> 11 passed.
  • uv run --frozen ruff check . -> 1 pre-existing F401 (Union unused in tools/geocoder.py:2) is unchanged from upstream/main and unrelated to this PR; all new code added in this PR is clean.
  • python3 -m py_compile tools/geocoder.py tools/staticmap.py tools/poisearch.py tests/test_tianditu.py -> OK.

Risks

  • None for behavior on the healthy path — successful 200 responses remain byte-identical.

Out of scope (deliberate)

  • tools/tianditu/tools/staticmap.py declares a class named PoiSearchTool even though the file is actually a static-map image tool (not POI search). This is a pre-existing naming mistake on main and is outside the scope of this bugfix PR.
  • The pre-existing Union unused import in tools/geocoder.py:2 is left alone to keep the diff strictly to the bug fixes.

References

Mirrors the same three-bug reliability pattern from PR langgenius#3456
(tools/google), and the matching PRs langgenius#3466 (tools/searchapi) and langgenius#3468
(tools/serper):

- requests.get had no timeout= argument in any of the three tool files
  (geocoder.py, staticmap.py, poisearch.py). Stalled hosts blocked until
  the framework-level 120s ceiling. Add timeout=10 to every call site.
- self.runtime.credentials["tianditu_api_key"] raised KeyError when the
  key was absent or empty. Replace with an early guard that yields a
  friendly error message and returns; use .get() for the value lookup.
- Wrap the HTTP calls in try/except requests.RequestException so a
  timeout, connection reset, DNS failure, or 5xx surfaces as a friendly
  message instead of a stack trace.

tianditu does not take user-supplied hl/gl (the geocoder API uses a
keyWord and the user's region comes from upstream), so the invoke
fall-through bug from PR langgenius#3456 does not apply.

No public API change; successful 200-path responses are byte-identical.

Adds tools/tianditu/tests/test_tianditu.py with 11 in-process test cases
covering all three tool files. Bumps manifest 0.0.7 -> 0.0.8.

Refactors nothing else; the staticmap.PoiSearchTool class name is kept
as-is to avoid unrelated churn. (It is mis-named — the file is the
static-map image tool, not POI search — but renaming is out of scope.)

Refs langgenius#3465
Refs langgenius#3456
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jul 20, 2026
@crazywoola
crazywoola requested a review from Copilot July 21, 2026 01:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds reliability safeguards to Tianditu tools.

Changes:

  • Adds credential guards, 10-second request timeouts, and network-error handling.
  • Adds in-process regression tests.
  • Bumps plugin version to 0.0.8.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
tools/tianditu/tools/geocoder.py Hardens geocoding requests.
tools/tianditu/tools/staticmap.py Hardens static-map requests.
tools/tianditu/tools/poisearch.py Hardens POI requests.
tools/tianditu/tests/test_tianditu.py Adds regression coverage.
tools/tianditu/tests/__init__.py Marks the test package.
tools/tianditu/manifest.yaml Bumps the patch version.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tools/tianditu/tools/geocoder.py Outdated
Comment on lines +28 to +31
result = requests.get(
base_url + "?ds=" + json.dumps(params, ensure_ascii=False) + "&tk=" + tk,
timeout=10,
).json()
Comment thread tools/tianditu/tools/geocoder.py Outdated
Comment on lines +32 to +35
except requests.exceptions.RequestException as exc:
yield self.create_text_message(
f"An error occurred while invoking the tool: {exc}."
)
Comment thread tools/tianditu/tools/staticmap.py Outdated
Comment on lines +51 to +54
except requests.exceptions.RequestException as exc:
yield self.create_text_message(
f"An error occurred while invoking the tool: {exc}."
)
Comment thread tools/tianditu/tools/poisearch.py Outdated
Comment on lines +58 to +61
except requests.exceptions.RequestException as exc:
yield self.create_text_message(
f"An error occurred while invoking the tool: {exc}."
)
Comment thread tools/tianditu/tools/staticmap.py Outdated
Comment on lines +32 to +35
+ "&tk="
+ tk,
timeout=10,
).json()
Comment thread tools/tianditu/tools/staticmap.py Outdated
Comment on lines +49 to +50
timeout=10,
).content
Comment thread tools/tianditu/tools/poisearch.py Outdated
Comment on lines +36 to +39
+ "&tk="
+ tk,
timeout=10,
).json()
Comment thread tools/tianditu/tools/poisearch.py Outdated
Comment on lines +55 to +57
+ tk,
timeout=10,
).json()
…sages

Copilot review flagged two issues on PR langgenius#3469:
- requests.get skipped raise_for_status(), so 4xx/5xx were treated as success.
- str(exc) in the yielded message can include the request URL with tk.

Add raise_for_status() before parsing each response. Use a generic
network/upstream failure message that never forwards URLs or credentials.

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

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants