Skip to content

Fix mutable default arguments#5329

Closed
Oxygen56 wants to merge 1 commit into
mem0ai:mainfrom
Oxygen56:fix/mutable-defaults-5301
Closed

Fix mutable default arguments#5329
Oxygen56 wants to merge 1 commit into
mem0ai:mainfrom
Oxygen56:fix/mutable-defaults-5301

Conversation

@Oxygen56
Copy link
Copy Markdown

Summary

  • replace mutable default values in Completions.create and BaseEmbedderConfig
  • preserve AzureConfig compatibility while creating a fresh default config
  • add regression coverage for isolated defaults

Testing

  • python3 -m py_compile mem0/proxy/main.py mem0/configs/embeddings/base.py tests/test_proxy.py tests/embeddings/test_azure_openai_embeddings.py
  • Not run: pytest, because this local Python environment is missing pytest and project test dependencies.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 31, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown

@biswajeetdev biswajeetdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on both instances. The mutable default argument antipattern in Python means the same list or dict object is shared across all calls, so mutations in one call silently affect subsequent calls — a notoriously hard bug to track down in production.

The messages: Optional[List] = None fix with messages = messages or [] inside the body is the standard corrective pattern. One small thing: messages or [] treats an explicitly passed empty list as falsy and replaces it with a new []. This is fine here since an empty message list is semantically equivalent, but if messages is None: messages = [] is more precise if callers might explicitly pass [] to mean "no messages" in a distinguishable way from not passing the arg at all.

The test using inspect.signature(Completions.create).parameters["messages"].default is None is a nice way to guard this at the API boundary — it will catch any future regression without needing to actually instantiate and mutate the class.

@Oxygen56
Copy link
Copy Markdown
Author

Oxygen56 commented Jun 2, 2026

Thanks for the review @biswajeetdev! I've reviewed the suggestion about if messages is None vs messages = messages or [].

In this specific case, the two are behaviorally equivalent since an explicitly-passed empty list is semantically the same as "no messages" — there's no code path that meaningfully distinguishes between them. That said, I see the style preference argument and am happy to switch to the more explicit if messages is None: messages = [] if you prefer — just let me know.

Otherwise, would love to get this merged when you have a moment. The mutable default issue, while not actively causing bugs today, is one of those things that silently bites the next person who adds a mutation downstream.

@kartik-mem0
Copy link
Copy Markdown
Contributor

Closing as duplicate — the fix for this was merged via #5302. Thank you for the contribution!

@kartik-mem0 kartik-mem0 closed this Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants