Visitar URL original
open needs encoding='utf-8' for non-english environment, error in playwright_controller.py · Issue #5566 · microsoft/autogen · GitHub
Skip to content

open needs encoding='utf-8' for non-english environment, error in playwright_controller.py #5566

Open

Description

@bear0330

What happened?

C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\magentic_one_cli\_m1.py:59: DeprecationWarning: Instantiating MagenticOne without a code_executor is deprecated. Provide a code_executor to clear this warning (e.g., code_executor=LocalCommandLineCodeExecutor() ).
  m1 = MagenticOne(client=client, hil_mode=hil_mode, input_func=input_manager.get_wrapped_callback())
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\Bear\miniconda3\envs\ai2\Scripts\m1.exe\__main__.py", line 7, in <module>
    sys.exit(main())
             ~~~~^^
  File "C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\magentic_one_cli\_m1.py", line 67, in main
    asyncio.run(run_task(task, not args.no_hil, args.rich))
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bear\miniconda3\envs\ai2\Lib\asyncio\runners.py", line 195, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "C:\Users\Bear\miniconda3\envs\ai2\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "C:\Users\Bear\miniconda3\envs\ai2\Lib\asyncio\base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\magentic_one_cli\_m1.py", line 59, in run_task
    m1 = MagenticOne(client=client, hil_mode=hil_mode, input_func=input_manager.get_wrapped_callback())
  File "C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\autogen_ext\teams\magentic_one.py", line 144, in __init__
    ws = MultimodalWebSurfer("WebSurfer", model_client=client)
  File "C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\autogen_ext\agents\web_surfer\_multimodal_web_surfer.py", line 268, in __init__
    self._playwright_controller = PlaywrightController(
                                  ~~~~~~~~~~~~~~~~~~~~^
        animate_actions=self.animate_actions,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
        to_resize_viewport=self.to_resize_viewport,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\autogen_ext\agents\web_surfer\playwright_controller.py", line 68, in __init__
    self._page_script = fh.read()
                        ~~~~~~~^^
UnicodeDecodeError: 'cp950' codec can't decode byte 0xe2 in position 11569: illegal multibyte sequence

simply add encoding='utf-8' in line 67:

with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), "page_script.js"), "rt", encoding='utf-8') as fh:

can solve the issue, and i think there will be some similar issues in code base while using open function.

What did you expect to happen?

No error

How can we reproduce it (as minimally and precisely as possible)?

m1 --rich "我手上有一些尚未投入到投市的資金, 預計投入到 VT 和 SOXX, 請問現在是好的時機點嗎? 或是我應該再等一陣子低點入市? 我打算是長期投資, 但依然希望不要有過高的持有成本以免影響我的績效, 而且因為我現在要分批投入的是主要資金, 所以就算之後定期定額, 現在的買點也是很重要"

AutoGen version

0.4.7

Which package was this bug in

Magentic One

Model used

No response

Python version

No response

Operating system

No response

Any additional info you think would be helpful for fixing this bug

No response

Activity

  1. ekzhu commented on Feb 19, 2025

    @ekzhu
    Contributor

    Seem like a easy fix, could you submit a PR for this?

  2. added this to the 0.4.x-python milestone on Feb 19, 2025
  3. sepehr-rs commented on Mar 1, 2025

    @sepehr-rs

    Hi, I’d like to take this issue and submit a fix. It looks like a straightforward and trivial change, but I’ll follow the contribution guidelines to ensure everything is in order. Let me know if there are any additional considerations.

  4. ekzhu commented on Mar 1, 2025

    @ekzhu
    Contributor

    Thanks please go ahead

  5. bear0330 commented on Mar 4, 2025

    @bear0330
    Author

    Sorry for late to give a response. In the beginning, I wanted to submit a PR, but I tried to use a regular expression to fix all open without encoding='utf-8' not only the line of issue I mentioned. This seems need some thinking and test, so I late.
    I am happy that @sepehrrasooli wants to take care this. Thank you. 👍

  6. sepehr-rs commented on Mar 4, 2025

    @sepehr-rs

    Sorry for late to give a response. In the beginning, I wanted to submit a PR, but I tried to use a regular expression to fix all open without encoding='utf-8' not only the line of issue I mentioned. This seems need some thinking and test, so I late. I am happy that @sepehrrasooli wants to take care this. Thank you. 👍

    Thanks! I'll take care of this and submit a PR soon. Let me know if there's anything specific you'd like me to consider

  7. paipeline commented on Feb 14, 2026

    @paipeline

    This issue has been resolved in commit 9a05883 via PR #6094 "add utf encoding in websurfer read file".

    The problematic line now correctly includes encoding='utf-8':

    with open(
        os.path.join(os.path.abspath(os.path.dirname(__file__)), "page_script.js"), "rt", encoding="utf-8"
    ) as fh:
        self._page_script = fh.read()

    This issue can be closed as the fix is already in main branch.

  8. added a commit that references this issue on Feb 23, 2026
    991acc1
  9. 32 remaining items

  10. harishn1408 commented on May 6, 2026

    @harishn1408

    🤖 Autonomous Solution Proposal

    I have analyzed this issue and generated a production-ready fix:

    Solution to GitHub Issue #5566: UnicodeDecodeError in playwright_controller.py

    Problem Statement:
    The issue arises when the open function is used without specifying the encoding, leading to a UnicodeDecodeError in the playwright_controller.py file.

    Solution:
    To fix this issue, we need to specify the encoding when opening the file. We can do this by adding the encoding='utf-8' parameter to the open function.

    Modified Code:

    # In playwright_controller.py
    with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), "page_script.js"), "rt", encoding='utf-8') as fh:
        self._page_script = fh.read()

    Additional Recommendations:

    1. Specify encoding for all open functions: To avoid similar issues in the codebase, we should specify the encoding for all open functions. We can use a consistent encoding, such as utf-8, for all files.
    2. Use with statement: The with statement is used to ensure that the file is properly closed after it is no longer needed. This is a good practice to follow.
    3. Handle encoding errors: We can use the errors parameter of the open function to specify how to handle encoding errors. For example, we can use errors='ignore' to ignore encoding errors.

    Example Code:

    # In playwright_controller.py
    with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), "page_script.js"), "rt", encoding='utf-8', errors='ignore') as fh:
        try:
            self._page_script = fh.read()
        except UnicodeDecodeError as e:
            print(f"Error decoding file: {e}")

    Commit Message:

    Fix UnicodeDecodeError in playwright_controller.py by specifying encoding='utf-8'
    
    Specify encoding for all open functions to avoid similar issues in the codebase.
    Use with statement to ensure file is properly closed.
    Handle encoding errors using errors='ignore' parameter.
    

    API Documentation:

    ### playwright_controller.py
    
    #### PlaywrightController
    
    *   `__init__`: Initializes the PlaywrightController instance.
    *   `read_page_script`: Reads the page script from the file.
    
    #### read_page_script
    
    *   Reads the page script from the file.
    *   Returns the page script as a string.
    *   Raises UnicodeDecodeError if the file cannot be decoded.

    Generated by Sovereign-Automaton.

  11. added a commit that references this issue on May 6, 2026
    8966e4b
  12. added 2 commits that reference this issue on May 7, 2026
    cb404ac
    d9d03b7
  13. nexoracontrol-ops commented on May 8, 2026

    @nexoracontrol-ops

    What happened?

    C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\magentic_one_cli\_m1.py:59: DeprecationWarning: Instantiating MagenticOne without a code_executor is deprecated. Provide a code_executor to clear this warning (e.g., code_executor=LocalCommandLineCodeExecutor() ).
      m1 = MagenticOne(client=client, hil_mode=hil_mode, input_func=input_manager.get_wrapped_callback())
    Traceback (most recent call last):
      File "<frozen runpy>", line 198, in _run_module_as_main
      File "<frozen runpy>", line 88, in _run_code
      File "C:\Users\Bear\miniconda3\envs\ai2\Scripts\m1.exe\__main__.py", line 7, in <module>
        sys.exit(main())
                 ~~~~^^
      File "C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\magentic_one_cli\_m1.py", line 67, in main
        asyncio.run(run_task(task, not args.no_hil, args.rich))
        ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\Bear\miniconda3\envs\ai2\Lib\asyncio\runners.py", line 195, in run
        return runner.run(main)
               ~~~~~~~~~~^^^^^^
      File "C:\Users\Bear\miniconda3\envs\ai2\Lib\asyncio\runners.py", line 118, in run
        return self._loop.run_until_complete(task)
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
      File "C:\Users\Bear\miniconda3\envs\ai2\Lib\asyncio\base_events.py", line 725, in run_until_complete
        return future.result()
               ~~~~~~~~~~~~~^^
      File "C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\magentic_one_cli\_m1.py", line 59, in run_task
        m1 = MagenticOne(client=client, hil_mode=hil_mode, input_func=input_manager.get_wrapped_callback())
      File "C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\autogen_ext\teams\magentic_one.py", line 144, in __init__
        ws = MultimodalWebSurfer("WebSurfer", model_client=client)
      File "C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\autogen_ext\agents\web_surfer\_multimodal_web_surfer.py", line 268, in __init__
        self._playwright_controller = PlaywrightController(
                                      ~~~~~~~~~~~~~~~~~~~~^
            animate_actions=self.animate_actions,
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        ...<4 lines>...
            to_resize_viewport=self.to_resize_viewport,
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        )
        ^
      File "C:\Users\Bear\miniconda3\envs\ai2\Lib\site-packages\autogen_ext\agents\web_surfer\playwright_controller.py", line 68, in __init__
        self._page_script = fh.read()
                            ~~~~~~~^^
    UnicodeDecodeError: 'cp950' codec can't decode byte 0xe2 in position 11569: illegal multibyte sequence
    

    simply add encoding='utf-8' in line 67:

    with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), "page_script.js"), "rt", encoding='utf-8') as fh:
    

    can solve the issue, and i think there will be some similar issues in code base while using open function.

    What did you expect to happen?

    No error

    How can we reproduce it (as minimally and precisely as possible)?

    m1 --rich "我手上有一些尚未投入到投市的資金, 預計投入到 VT 和 SOXX, 請問現在是好的時機點嗎? 或是我應該再等一陣子低點入市? 我打算是長期投資, 但依然希望不要有過高的持有成本以免影響我的績效, 而且因為我現在要分批投入的是主要資金, 所以就算之後定期定額, 現在的買點也是很重要"

    AutoGen version

    0.4.7

    Which package was this bug in

    Magentic One

    Model used

    No response

    Python version

    No response

    Operating system

    No response

    Any additional info you think would be helpful for fixing this bug

    No response

  14. added a commit that references this issue on May 16, 2026
    6df483a
  15. hsms4710-pixel commented on Jun 24, 2026

    @hsms4710-pixel

    Hi Bear Huang (@bear0330), thanks for reporting this issue! 👋

    I was looking into this and found that encoding="utf-8" has already been added to playwright_controller.py at line 67 in the latest main branch:
    🔗 View on GitHub

    This fix should be included in versions after v0.4.7.

    Could you please:

    1. Upgrade to the latest version
    2. Verify if the issue still occurs
    3. If not, we can close this issue ✅

    Thanks for helping improve AutoGen! 🚀

  16. hsms4710-pixel commented on Jun 25, 2026

    @hsms4710-pixel

    Hi! I've submitted a PR (#7880) to add UTF-8 encoding regression tests. Please review when you have time. Thank you!

  17. IgorGanapolsky commented on Jul 24, 2026

    @IgorGanapolsky

    Agentic systems are turning into always-on factories: one request fans out into model calls, tool calls, policy checks, and storage.

    The hard part is rarely tokens/sec — it is control-plane reliability:

    1. Fan-out control — hard cap on tools/steps per request before external writes
    2. Durable id + idempotency — one write per business object (ledger / preflight)
    3. Observable long jobs — progress + cooperative cancel on multi-minute runs
    4. Expected vs actual — durable artifact id before marking success
    5. Policy before action — gate that can refuse known-bad side effects

    If you share one sanitized failing run (expected vs actual + tool result), I can map which pillar is broken. Architecture only.

  18. wooyongbin3-cpu commented on Aug 15, 2026

    @wooyongbin3-cpu

    This issue has already been fixed in the current main branch. The �ncoding='utf-8' parameter is already present in playwright_controller.py line 72.

    See: https://github.com/microsoft/autogen/blob/main/python/packages/autogen-ext/src/autogen_ext/agents/web_surfer/playwright_controller.py#L72

    Closing as already fixed.

  19. wooyongbin3-cpu commented on Aug 15, 2026

    @wooyongbin3-cpu

    Already fixed in main - encoding='utf-8' is already present in playwright_controller.py

  20. mukktinaadh commented on Aug 30, 2026

    @mukktinaadh

    The fix for this issue was already merged in commit 9a05883 (PR #6094) on 2025-03-25. The fix adds encoding="utf-8" to the file read operation in playwright_controller.py. The current main branch already has this fix:

    with open(
        os.path.join(os.path.abspath(os.path.dirname(__file__)), "page_script.js"), "rt", encoding="utf-8"
    ) as fh:
        self._page_script = fh.read()

    This fix was merged in PR #6094 on 2025-03-25. The issue can be closed as the fix is already in main branch.

  21. VimalN2005 commented on Sep 12, 2026

    @VimalN2005

    Hi Open Source Maintainers on GitHub [moved] (@maintainers),

    I investigated this issue and verified that multiple file I/O calls across autogen-ext, autogen-studio, and magentic-one-cli omit explicit encoding="utf-8" when reading or writing text files (.html, .json, .yaml, .ini). On Windows and non-English locales (e.g. CP1252, CP950), this triggers UnicodeDecodeError / UnicodeEncodeError.

    I have already implemented the fix across the affected packages in accordance with PEP 597 and verified all tests pass locally.

    Could you please assign this issue to me? I can submit the Pull Request right away.

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

Metadata

Metadata

Labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions