Skip to content

Bug: UnicodeEncodeError on Windows when materializing skill resources in _SkillScriptCodeExecutor #5819

@mc-marcocheng

Description

@mc-marcocheng

🔴 Required Information

Describe the Bug:
When running the ADK on Windows, executing a skill script via run_skill_script (or RunSkillScriptTool) fails if the skill's reference files (references/*), assets, or scripts contain non-ASCII characters (like Chinese characters). The crash happens inside _SkillScriptCodeExecutor._build_wrapper_code because the generated self-extracting wrapper script writes text files without specifying an encoding, causing Windows to fall back to the system locale encoding (e.g., cp1252) and fail when writing UTF-8 characters.

Steps to Reproduce:

  1. Run the ADK on a Windows OS.
  2. Create a Skill object that includes a reference file, script, or asset containing non-ASCII characters (e.g., "你好,世界").
  3. Use the _SkillScriptCodeExecutor (or RunSkillScriptTool) to execute the skill script.
  4. The execution will immediately fail before the actual script runs.

Expected Behavior:
The self-extracting wrapper script should correctly materialize text files containing non-ASCII characters using utf-8 encoding and proceed to run the target script.

Observed Behavior:
The code executor fails immediately with a UnicodeEncodeError.

Traceback (most recent call last):
  File "C:\Users\...\AppData\Local\Temp\tmp7dawkex_.py", line 27, in <module>
    _materialize_and_run()
  File "C:\Users\...\AppData\Local\Temp\tmp7dawkex_.py", line 16, in _materialize_and_run
    f.write(content)
  File "C:\Users\...\AppData\Roaming\uv\python\cpython-3.13.2-windows-x86_64-none\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-4: character maps to <undefined>

Environment Details:

  • ADK Library Version: main branch (source)
  • Desktop OS: Windows
  • Python Version: 3.13.2

Model Information:

  • Are you using LiteLLM: N/A
  • Which model is being used: N/A

🟡 Optional Information

Regression:
N/A

Logs:
N/A

Screenshots / Video:
N/A

Additional Context:
The fix is to enforce encoding='utf-8' when mode == 'w' in google/adk/tools/skill_toolset.py.

Minimal Reproduction Code:

import tempfile
import subprocess
import sys
from google.adk.skills.models import Skill, Frontmatter, Resources, Script
from google.adk.tools.skill_toolset import _SkillScriptCodeExecutor

class MockExecutor: pass
executor = _SkillScriptCodeExecutor(base_executor=MockExecutor(), script_timeout=10)

skill = Skill(
    frontmatter=Frontmatter(name="test-skill", description="test"),
    instructions="test",
    resources=Resources(
        references={"unicode.txt": "你好,世界"},
        scripts={"run.py": Script(src="print('Success')")}
    )
)

wrapper_code = executor._build_wrapper_code(skill=skill, file_path="run.py", script_args=None)

with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, encoding='utf-8') as tf:
    tf.write(wrapper_code)

result = subprocess.run([sys.executable, tf.name], capture_output=True, text=True)
print(result.stderr) # Throws UnicodeEncodeError on Windows

How often has this issue occurred?:

  • Always (100%) when non-ASCII characters are present on Windows.

Metadata

Metadata

Assignees

Labels

tools[Component] This issue is related to tools

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions