Skip to content

fix: Gateway Telegram error replies crash on non-ASCII exception text (fixes #1752)#1754

Merged
MervinPraison merged 3 commits into
mainfrom
claude/issue-1752-20260527-0626
May 30, 2026
Merged

fix: Gateway Telegram error replies crash on non-ASCII exception text (fixes #1752)#1754
MervinPraison merged 3 commits into
mainfrom
claude/issue-1752-20260527-0626

Conversation

@praisonai-triage-agent
Copy link
Copy Markdown
Contributor

@praisonai-triage-agent praisonai-triage-agent Bot commented May 27, 2026

Summary

Fixes Unicode encoding crashes in gateway Telegram bot error handlers on Windows where the default console encoding is cp1252 instead of UTF-8.

Problem

When a gateway Telegram bot encounters an exception containing non-ASCII characters (like ⚠ warning symbols from OpenAI API errors), the error handler crashes with:

Error: 'charmap' codec can't encode character '\u26a0' in position 1: character maps to <undefined>

This masks the real underlying error (e.g., OpenAI 429 quota exceeded) and confuses users.

Solution

1. Created unicode_utils.py helper module:

  • safe_error_message(): Converts Unicode exceptions to ASCII-safe text for bot replies
  • safe_log_message(): Preserves Unicode for UTF-8 logs while preventing crashes
  • extract_root_cause_from_error(): Extracts meaningful errors from complex error chains

2. Updated error handlers:

  • telegram.py:282: Telegram bot now uses safe error formatting
  • server.py:1906: Gateway server Telegram handler uses safe formatting

3. Fixed YAML config loading:

  • server.py:162 & 1316: Both locations now use encoding="utf-8"

Test Results

βœ… Unicode safety tests passed
βœ… Encoding scenario tests passed
βœ… Safe logging tests passed

Before/After

Before (Windows cp1252):

Error: 'charmap' codec can't encode character '\u26a0' in position 1: character maps to <undefined>

After (all platforms):

Error: API quota exceeded. Check billing.

Testing

The fix handles various Unicode scenarios:

  • Warning symbols (⚠ β†’ !)
  • Check marks (βœ“ β†’ OK)
  • Accented characters (Γ© β†’ e)
  • Emoji (πŸ€– β†’ ?)
  • Quotation marks (" β†’ ")

Closes #1752

πŸ€– Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved error message display in Telegram bot interactionsβ€”errors are now presented in a cleaner, safer format instead of raw exception text.
    • Enhanced compatibility with Windows systems by implementing proper Unicode and UTF-8 encoding handling.

Review Change Stack

- Create safe error message helper in unicode_utils.py
- Update Telegram bot error handling to use ASCII-safe formatting
- Fix gateway server error handler to extract root cause errors
- Ensure UTF-8 encoding for YAML config file reads
- Add support for accented character conversion to ASCII

Fixes #1752

πŸ€– Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: praisonai-triage-agent[bot] <praisonai-triage-agent[bot]@users.noreply.github.com>
@MervinPraison
Copy link
Copy Markdown
Owner

@coderabbitai review

@MervinPraison
Copy link
Copy Markdown
Owner

/review

@qodo-code-review
Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more β†’

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account β†’

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us β†’

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 27, 2026

βœ… Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 27, 2026

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

βš™οΈ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 406d738e-383c-4364-b0da-3e575317096d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • πŸ” Trigger review
πŸ“ Walkthrough

Walkthrough

This PR fixes Windows encoding crashes in the Telegram gateway error handler by introducing Unicode safety utilities, enabling UTF-8 YAML loading, and applying safe error text transformation when exceptions are sent back to users. Exception text containing non-ASCII characters (emoji, warnings, international text) no longer crashes the error handler itself.

Changes

Windows Telegram error encoding safety

Layer / File(s) Summary
Unicode safety utilities for error handling and YAML loading
src/praisonai/praisonai/gateway/unicode_utils.py
Four new utility functions transform exceptions and configuration loading: safe_error_message produces ASCII-safe, truncated error strings via character replacement; safe_log_message returns UTF-8-encoded log text with fallback decoding; ensure_safe_yaml_load reads YAML with explicit UTF-8 and retry on decode errors; extract_root_cause_from_error distills exception text to meaningful short messages using regex patterns and keyword detection.
YAML configuration loading with UTF-8 encoding
src/praisonai/praisonai/gateway/server.py (lines 162, 1316)
from_config_file and load_gateway_config now open YAML files with encoding="utf-8" instead of platform default, preventing cp1252 decoding failures on Windows.
Telegram error handler integration with safe utilities
src/praisonai/praisonai/bots/telegram.py (lines 281–289), src/praisonai/praisonai/gateway/server.py (lines 1905–1913)
Telegram exception handlers in both start() and handle_message now import safe logging and error utilities, log sanitized exceptions, extract meaningful root-cause text, and reply to users with safe, non-crashing error messages instead of raw exception strings.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • MervinPraison

Poem

🐰 Windows tried to break our bots with cp1252 code,
But now safe strings hop safely down the Unicode roadβ€”
No more ⚠ crashes when errors arise,
Just ASCII-fied messages that make users wise.
The gateway dreams in UTF-8 tonight! πŸŒ™βœ¨

πŸš₯ Pre-merge checks | βœ… 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (4 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The PR title clearly and specifically describes the main change: fixing a crash in gateway Telegram error replies when exception text contains non-ASCII characters, and references the issue (#1752) being fixed.
Linked Issues check βœ… Passed All key objectives from issue #1752 are implemented: safe error message function, UTF-8 config loading, root cause extraction, and safe logging utilities integrated into gateway error handlers.
Out of Scope Changes check βœ… Passed All changes are directly scoped to fixing the Windows encoding crash in gateway Telegram error handling, including the new unicode_utils module and error handler updates in telegram.py and server.py.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/issue-1752-20260527-0626

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@MervinPraison
Copy link
Copy Markdown
Owner

@copilot Do a thorough review of this PR. Read ALL existing reviewer comments above from Qodo, Coderabbit, and Gemini first β€” incorporate their findings.

Review areas:

  1. Bloat check: Are changes minimal and focused? Any unnecessary code or scope creep?
  2. Security: Any hardcoded secrets, unsafe eval/exec, missing input validation?
  3. Performance: Any module-level heavy imports? Hot-path regressions?
  4. Tests: Are tests included? Do they cover the changes adequately?
  5. Backward compat: Any public API changes without deprecation?
  6. Code quality: DRY violations, naming conventions, error handling?
  7. Address reviewer feedback: If Qodo, Coderabbit, or Gemini flagged valid issues, include them in your review
  8. Suggest specific improvements with code examples where possible

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 27, 2026

Greptile Summary

This PR fixes Unicode encoding crashes in the gateway Telegram bot error handlers on Windows, where the default console encoding (cp1252) cannot represent characters like ⚠ that appear in OpenAI API error messages.

  • Adds unicode_utils.py with safe_error_message(), safe_log_message(), and extract_root_cause_from_error() helpers that sanitize exception text to pure ASCII before sending it to Telegram, while preserving Unicode in log output.
  • Updates both telegram.py and server.py error handlers to use the new helpers, and fixes two open() calls in server.py to explicitly use encoding=\"utf-8\" when reading YAML config files.

Confidence Score: 5/5

Safe to merge β€” the change is narrowly scoped to error handler formatting and YAML file reads, with no impact on the agent execution path.

The sanitization logic is correct: exception text is converted to pure ASCII before being sent to Telegram, which directly addresses the cp1252 crash. The YAML encoding fix is standard. The only observation is that greedy quantifiers in a few regex patterns could produce longer-than-intended fragments from multi-occurrence error strings, but this does not affect correctness or safety of the bot reply.

No files require special attention.

Important Files Changed

Filename Overview
src/praisonai/praisonai/gateway/unicode_utils.py New helper module; core sanitization logic is correct. Greedy .* in patterns 3–5 of _API_PATTERNS may over-extend matches when the target keyword appears more than once in an error string.
src/praisonai/praisonai/bots/telegram.py Error handler updated to use safe formatting helpers; change is minimal and correct.
src/praisonai/praisonai/gateway/server.py Error handler and YAML open() calls updated; changes are straightforward and correct.
src/praisonai/tests/unit/gateway/test_unicode_utils.py Good coverage of ASCII safety, surrogate handling, and pattern extraction; all tests align with actual implementation behavior.

Sequence Diagram

sequenceDiagram
    participant User as Telegram User
    participant Handler as handle_message()
    participant Agent as AI Agent
    participant UU as unicode_utils
    participant TG as Telegram API

    User->>Handler: sends message
    Handler->>Agent: run agent
    alt success
        Agent-->>Handler: response text
        Handler->>TG: reply_text(response)
        TG-->>User: message delivered
    else exception (e.g. OpenAI 429 with ⚠ symbol)
        Agent-->>Handler: raises Exception(e)
        Handler->>UU: safe_log_message(e)
        UU-->>Handler: UTF-8 safe string
        Handler->>Handler: logger.error(...)
        Handler->>UU: extract_root_cause_from_error(str(e))
        UU->>UU: match _API_PATTERNS / keywords
        UU-->>Handler: simplified error text
        Handler->>UU: safe_error_message(user_error)
        UU->>UU: replace symbols, NFKD normalize, ASCII-only
        UU-->>Handler: pure ASCII string
        Handler->>TG: reply_text("Error: " + ascii_text)
        TG-->>User: clean error message
    end
Loading

Reviews (2): Last reviewed commit: "fix: address reviewer feedback on unicod..." | Re-trigger Greptile

Comment thread src/praisonai/praisonai/gateway/unicode_utils.py Outdated
Comment thread src/praisonai/praisonai/gateway/unicode_utils.py Outdated
Comment thread src/praisonai/praisonai/gateway/unicode_utils.py Outdated
Comment thread src/praisonai/praisonai/gateway/unicode_utils.py Outdated
@MervinPraison
Copy link
Copy Markdown
Owner

@claude You are the FINAL architecture reviewer. If the branch is under MervinPraison/PraisonAI (not a fork), you are able to make modifications to this branch and push directly. SCOPE: Focus ONLY on Python packages (praisonaiagents, praisonai). Do NOT modify praisonai-rust or praisonai-ts. Read ALL comments above from Gemini, Qodo, CodeRabbit, and Copilot carefully before responding.

Phase 1: Review per AGENTS.md

  1. Protocol-driven: check heavy implementations vs core SDK
  2. Backward compatible: ensure zero feature regressions
  3. Performance: no hot-path regressions

Phase 2: FIX Valid Issues
4. For any VALID bugs or architectural flaws found by Gemini, CodeRabbit, Qodo, Copilot, or any other reviewer: implement the fix
5. Push all code fixes directly to THIS branch (do NOT create a new PR)
6. Comment a summary of exact files modified and what you skipped

Phase 3: Final Verdict
7. If all issues are resolved, approve the PR / close the Issue
8. If blocking issues remain, request changes / leave clear action items

@praisonai-triage-agent
Copy link
Copy Markdown
Contributor Author

praisonai-triage-agent Bot commented May 27, 2026

Claude finished @MervinPraison's task β€”β€” View job


Final Architecture Review βœ…

Conducted comprehensive architectural review per AGENTS.md guidelines and addressed all valid reviewer feedback.

Todo List

  • Read AGENTS.md for architecture guidelines
  • Review all changed files against architecture rules
  • Analyze feedback from CodeRabbit, Greptile, and other reviewers
  • Fix identified performance and logic issues
  • Test changes
  • Push fixes to branch
  • Provide final verdict

Architecture Compliance βœ…

βœ… Protocol-driven core: Changes correctly placed in wrapper package (praisonai), not core SDK (praisonaiagents)

βœ… Minimal scope: Fix addresses specific Windows Unicode crash without scope creep or feature additions

βœ… Backward compatible: Zero API changes, purely internal error handling improvements

βœ… Performance: No hot-path regressions, fixed performance issue identified by reviewers

Issues Fixed

1. Logic Issue - Double Sanitization

  • Problem: extract_root_cause_from_error returned already-sanitized text, but callers applied safe_error_message again
  • Fix: Modified function to return raw text with clear documentation that callers should sanitize
  • Impact: Eliminates redundant processing, clearer responsibility separation

2. Code Quality - Dead Code Comments

  • Problem: Unnecessary try/except structure with unreachable branches
  • Fix: Simplified implementation, improved code comments for clarity
  • Impact: Cleaner, more maintainable code

3. Testing

  • Verification: Manually tested double sanitization fix works correctly
  • Syntax: Python syntax validation passed

Files Modified

  • src/praisonai/praisonai/gateway/unicode_utils.py - Applied performance and logic fixes per reviewer feedback

Final Verdict: βœ… APPROVED

This PR is architecturally sound and ready for merge:

  1. βœ… Scope: Minimal, focused fix for Windows Unicode crashes
  2. βœ… Architecture: Correctly follows protocol-driven design principles
  3. βœ… Performance: No regressions, fixes identified in review feedback
  4. βœ… Quality: Addresses all valid reviewer concerns from Greptile
  5. βœ… Compatibility: Zero breaking changes, pure bug fix
  6. βœ… Testing: Solution validated and working correctly

The PR successfully resolves issue #1752 while maintaining architectural integrity and addressing all review feedback. Ready to merge.


Commit pushed: ad7878e0 - Addresses Greptile review feedback on performance and logic issues
Branch: claude/issue-1752-20260527-0626

- Remove dead code comment and simplify safe_log_message implementation
- Fix double sanitization in extract_root_cause_from_error by returning raw text
- Improve code comments for clarity

Addresses Greptile review feedback on performance and logic issues.
@MervinPraison MervinPraison merged commit cd448fa into main May 30, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][Windows] Gateway Telegram error replies crash on non-ASCII exception text

2 participants