Skip to content

Bug: BedrockAgentFunctionResolver default serializer escapes non-ASCII characters, degrading agent output #7776

@xming-cn

Description

@xming-cn

Summary

BedrockAgentFunctionResolver currently uses json.dumps as its default serializer without setting ensure_ascii=False.
As a result, all non-ASCII characters (Chinese, Japanese, emoji, etc.) are escaped into \uXXXX sequences.

This significantly reduces the quality and readability of tool outputs in Bedrock Agent workflows, and can negatively impact LLM reasoning, especially for multilingual scenarios.

Additionally, the current documentation does not mention that the serializer is configurable, making this behavior surprising and difficult for users to diagnose.

Why this is a problem

  • LLM agents typically expect natural UTF-8 output, not escaped Unicode.
  • Bedrock models (Claude, Llama, Titan) all fully support Unicode — no need for escaping.
  • Escaped text makes responses harder to read and lowers the effectiveness of agent tools that return text.
  • Non-ASCII languages become difficult to use with the default resolver.
  • The serializer parameter is undocumented, so users have no indication that this behavior can be changed.

This behavior appears to be inherited from standard Python JSON defaults rather than intentional design for LLM agent workflows.

Expected Behaviour

The BedrockAgentFunctionResolver default output serializer should preserve Unicode characters, e.g.:

lambda body: json.dumps(body, ensure_ascii=False)

This aligns with typical expectations for modern LLM/agent systems where multilingual natural text is common.

Current Behaviour

Example:

return {"msg": "你好"}

becomes:

{"msg": "\u4f60\u597d"}

Code snippet

from aws_lambda_powertools.event_handler import BedrockAgentFunctionResolver

app = BedrockAgentFunctionResolver()

@app.tool(name="hello", description="Returns a greeting")
def hello():
    return {"msg": "你好"}

def lambda_handler(event, context):
    return app.resolve(event, context)

Output: {"msg": "\u4f60\u597d"}
Expected: {"msg": "你好"}

Current Workaround

It is possible to override the serializer manually:

import json
from aws_lambda_powertools.event_handler import BedrockAgentFunctionResolver

app = BedrockAgentFunctionResolver(
    serializer=lambda x: json.dumps(x, ensure_ascii=False)
)

This restores correct Unicode output.

However:

  1. This capability is undocumented.
  2. Users have no way to know the default serializer is the cause of the issue.
  3. The workaround is unintuitive for most agent-tool users.

Because of this, relying solely on the workaround is not ideal.

Possible Solution

Option A (recommended): Change default serializer

Use:

class BedrockAgentFunctionResolver:
    def __init__(self, serializer: Callable | None = None) -> None:
        self.serializer = serializer or (lambda x: json.dumps(x, ensure_ascii=False))

This preserves Unicode output while remaining fully JSON-compliant.

Option B: Document the serializer parameter

If changing the default is not preferred, documenting the serializer override capability would reduce confusion.

Steps to Reproduce

use code from the "Code snippet"

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.13

Packaging format used

Lambda Layers

Closing

If needed, I can implement the change and submit a PR.

This issue affects readability, multilingual support, and LLM reasoning quality.
Switching to ensure_ascii=False or providing a documented way to configure this behavior would greatly improve usability for Bedrock Agent tool builders.

Thank you for your work on the project!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriagePending triage from maintainers

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions