Skip to content

UnicodeEncodeError while deploying flows via code. #19083

@daniel-leicht

Description

@daniel-leicht

Bug summary

I have a script that deploys multiple flows into my Prefect server:

    # Upload the flow file and configure deployments:
    for flow_function, flow_file in FLOWS:
        flow_function_name = flow_function.__name__
        print(f"Deploying {flow_function_name} to process work pool")

        # To run the file from Minio with a specific image:
        flow_function.from_source(
            source=flow_files_storage,
            entrypoint=f"{flow_file}:{flow_function_name}",
        ).deploy(
            name=f"{flow_function_name} process deployment",
            work_pool_name=PROCESS_WORK_POOL,
            job_variables={
                "env": dict(os.environ),
            },
        )

Suddenly, after changing my environment, I get the following error when attempting to deploy the flows:

Looks like you're deploying to a process work pool. If you're creating a 
deployment for local development, calling `.serve` on your flow is a great way 
to get started. See the documentation for more information: 
https://docs.prefect.io/latest/how-to-guides/deployments/run-flows-in-local-pro
cesses  Set `ignore_warnings=True` to suppress this message.

Successfully created/updated all deployments!

                                  Deployments                                  
┌──────────────────────────────────────────────────────────────┬─────────┬────┐
│ Name                                                         │ Status  │ D… │
├──────────────────────────────────────────────────────────────┼─────────┼────┤
│ Minio to Bronze Sync/sync_minio_to_bronze process deployment │ applied │    │
└──────────────────────────────────────────────────────────────┴─────────┴────┘
python-BaseException
Traceback (most recent call last):
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\prefect\utilities\asyncutils.py", line 207, in run_coro_as_sync
    return call.result()
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\prefect\_internal\concurrency\calls.py", line 365, in result
    return self.future.result(timeout=timeout)
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\prefect\_internal\concurrency\calls.py", line 192, in result
    return self.__get_result()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\concurrent\futures\_base.py", line 390, in __get_result
    raise self._exception
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\prefect\_internal\concurrency\calls.py", line 441, in _run_async
    result = await coro
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\prefect\utilities\asyncutils.py", line 188, in coroutine_wrapper
    return await task
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\futures.py", line 284, in __await__
    yield self  # This tells Task to wait for completion.
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py", line 328, in __wakeup
    future.result()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\futures.py", line 201, in result
    raise self._exception
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py", line 256, in __step
    result = coro.send(None)
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\prefect\utilities\asyncutils.py", line 341, in ctx_call
    result = await async_fn(*args, **kwargs)
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\prefect\flows.py", line 1558, in deploy
    deployment_ids = await deploy_coro
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\prefect\utilities\asyncutils.py", line 341, in ctx_call
    result = await async_fn(*args, **kwargs)
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\prefect\deployments\runner.py", line 1323, in deploy
    console.print(table)
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\rich\console.py", line 1740, in print
    self._buffer.extend(new_segments)
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\rich\console.py", line 866, in __exit__
    self._exit_buffer()
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\rich\console.py", line 824, in _exit_buffer
    self._check_buffer()
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\rich\console.py", line 2033, in _check_buffer
    self._write_buffer()
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\rich\console.py", line 2081, in _write_buffer
    write(text)
  File "C:\Projects\BullzAI\NewETLPilot\venv\lib\site-packages\win32\scripts\pywin32_postinstall.py", line 33, in write
    tee_f.write(what)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\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 81-159: character maps to <undefined>
*** You may need to add PYTHONIOENCODING=utf-8 to your environment ***

I have attempted to debug the issue and see if there are any problematic characters in any of my files by placing a breakpoint and viewing the input value in the cp1252.py file in the encoding library:

class IncrementalEncoder(codecs.IncrementalEncoder):
    def encode(self, input, final=False):
        return codecs.charmap_encode(input,self.errors,encoding_table)[0]

The input value that caused the exception was actually the table that prefect generates:

                                  Deployments                                  
┌──────────────────────────────────────────────────────────────┬─────────┬────┐
│ Name                                                         │ Status  │ D… │
├──────────────────────────────────────────────────────────────┼─────────┼────┤
│ Minio to Bronze Sync/sync_minio_to_bronze process deployment │ applied │    │
└──────────────────────────────────────────────────────────────┴─────────┴────┘

Version info

Version:             3.4.7
API version:         0.8.4
Python version:      3.9.7
Git commit:          cd81d15a
Built:               Thu, Jun 26, 2025 09:16 PM
OS/Arch:             win32/AMD64
Profile:             ephemeral
Server type:         server
Pydantic version:    2.11.7
Integrations:
  prefect-aws:       0.5.11
  prefect-github:    0.3.1

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions