Skip to content

Feature request: Allow customizing ApiGatewayResolver serializer instantiation parameters #6394

Description

@alexminza

Use case

By default json.dumps is initialized with ensure_ascii: bool = True which escapes all non-ASCII output.
To overcome this, one has to recreate the internal serializer from scratch and pass it as a custom serializer:

serializer = functools.partial(json.dumps, separators=(",", ":"), cls=json_encoder.Encoder, ensure_ascii=False)
app = APIGatewayHttpResolver(serializer=serializer)

This hardcodes many of the library design choices on the app side.

Solution/User Experience

Allow passing additional parameters to the serializer initialization:

app = APIGatewayHttpResolver(serializer_kwargs={'ensure_ascii': False})

https://github.com/aws-powertools/powertools-lambda-python/blob/develop/aws_lambda_powertools/event_handler/api_gateway.py#L1553

def __init__(self, serializer_kwargs: dict):
    # Allow for a custom serializer or a concise json serialization
    self._serializer = serializer or partial(json.dumps, separators=(",", ":"), cls=Encoder, **serializer_kwargs)

Proposed solution is similar to the LangChain library design:
https://api.python.langchain.com/en/latest/chains/langchain.chains.llm.LLMChain.html#langchain.chains.llm.LLMChain.llm_kwargs

Alternative solutions

Hardcode serializer args:

self._serializer = serializer or partial(json.dumps, separators=(",", ":"), cls=Encoder, ensure_ascii=False)

Acknowledgment

Activity

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

Metadata

Metadata

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions