Conversation
Note that we still want to run tests, as these depend on the metadata.
|
🧪 Testing To try out this version of the SDK: Expires at: Fri, 01 May 2026 21:35:36 GMT |
d85451e to
c8199ba
Compare
| kwargs: _ModelDumpKwargs = {} | ||
| if by_alias is not None: | ||
| kwargs["by_alias"] = by_alias | ||
| return model.model_dump( | ||
| mode=mode, | ||
| exclude=exclude, |
There was a problem hiding this comment.
🔴 The _ModelDumpKwargs dict built in model_dump is never spread into model.model_dump() — the explicit by_alias=bool(by_alias) if by_alias is not None else False on line 159 remains, so by_alias=False is unconditionally passed to pydantic when the caller omits the argument. This completely defeats the stated fix ('pydantic: do not pass by_alias unless set') and silently overrides model-level alias_generator defaults in pydantic v2/v3; the fix is to replace the explicit by_alias kwarg with **kwargs.
Extended reasoning...
What the bug is
In src/openlayer/_compat.py, the PR introduces a _ModelDumpKwargs TypedDict and, inside the pydantic-v2 branch of model_dump, conditionally populates it:
kwargs: _ModelDumpKwargs = {}
if by_alias is not None:
kwargs["by_alias"] = by_aliasHowever, the subsequent model.model_dump() call still contains the old explicit argument:
return model.model_dump(
mode=mode,
exclude=exclude,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
warnings=True if PYDANTIC_V1 else warnings,
by_alias=bool(by_alias) if by_alias is not None else False, # never removed
)The kwargs dict is built but never spread — **kwargs is absent. It is completely dead code.
The specific code path that triggers it
Every call to model_dump(model) (the default case, by_alias=None) hits the pydantic v2 branch, builds an empty kwargs, and then calls model.model_dump(..., by_alias=False) because bool(by_alias) if by_alias is not None else False evaluates to False.
Why existing code does not prevent it
The kwargs TypedDict construction looks correct in isolation, which makes the bug easy to miss. The explicit by_alias=... keyword was never removed from the model.model_dump() call site when **kwargs was supposed to replace it. There are no tests verifying that by_alias is absent from forwarded kwargs when not explicitly set, so the regression went undetected.
Impact
Pydantic v2 models that rely on model-level alias configuration (e.g., model_config = ConfigDict(alias_generator=..., populate_by_name=True)) will have their alias behavior silently overridden whenever model_dump is called without an explicit by_alias argument. Fields will be serialized using their Python attribute names instead of the configured aliases, potentially breaking serialization for any SDK model that uses aliased field names.
How to fix it
Remove the explicit by_alias=bool(by_alias) if by_alias is not None else False kwarg and replace with **kwargs:
return model.model_dump(
mode=mode,
exclude=exclude,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
warnings=True if PYDANTIC_V1 else warnings,
**kwargs,
)Step-by-step proof
- Caller invokes
model_dump(my_model)with noby_aliasargument — defaults toNone. - Code enters the
if (not PYDANTIC_V1) or hasattr(model, "model_dump"):branch. kwargs: _ModelDumpKwargs = {}is created; theif by_alias is not None:guard isFalse, sokwargsremains{}.model.model_dump(..., by_alias=bool(None) if None is not None else False)simplifies tomodel.model_dump(..., by_alias=False).- Pydantic v2 receives an explicit
by_alias=False, overriding any model-levelalias_generatordefault. - The
kwargsdict is never referenced again and is garbage-collected at end of scope.
| fragment_template: str | None = None | ||
| query_template: str | None = None | ||
|
|
There was a problem hiding this comment.
🟡 Inside path_template(), the local variable path_template = rest (line ~107) shadows the enclosing function name for the rest of the function scope. While the current code works correctly because the name is never used after assignment to call the function, any future code added inside this function that tries to call path_template(...) would get a TypeError: str object is not callable instead of the expected function call. Rename the variable to path_str or path_portion to eliminate the shadowing.
Extended reasoning...
In src/openlayer/_utils/_path.py, the newly introduced function path_template contains this assignment near the end of its setup block:
path_template = restThis assigns a local string variable with the exact same name as the enclosing function. In Python, a name is determined to be local to a function at compile time - if Python sees any assignment to path_template anywhere in the function body, the entire function body treats path_template as a local variable. This means the module-level function object is completely inaccessible inside path_template() after the assignment.
The specific code path is: the function splits the input template into path, query, and fragment portions (rest = template, then optional splits on # and ?), and then assigns path_template = rest to hold the path portion. This string is then passed to _interpolate(path_template, kwargs, _quote_path_segment_part) on the very next statement.
The existing code is safe only because: (1) the function is not recursive and does not need to call itself, and (2) the local variable path_template (the string) is only accessed on the line immediately following its assignment, strictly after it has been bound. There is no window where an UnboundLocalError can occur in the current code.
However, this is an error-prone pattern with real maintenance risks. If a developer later adds code inside this function body and writes path_template(...), Python will raise TypeError: 'str' object is not callable (if after the assignment) or UnboundLocalError: local variable 'path_template' referenced before assignment (if before). Both errors would be confusing because the name visually looks like a function reference. Static analysis tools like pyright also flag this shadowing.
Step-by-step proof:
- Call
path_template("/v1/{id}", id="abc"). - Inside the function,
rest = "/v1/{id}", no#or?, so no splits occur. path_template = restassigns the string"/v1/{id}"to the local namepath_template._interpolate(path_template, ...)uses the local string - works fine currently.- If a developer adds
path_template(sub_template, ...)anywhere in this function after this PR, they will get a runtime error instead of a function call, because Python has already markedpath_templateas a local variable for the entire function scope.
Fix: rename the local variable to path_str or path_portion and update the single usage on the next line:
path_str = rest
path_result = _interpolate(path_str, kwargs, _quote_path_segment_part)|
🤖 Release is at https://github.com/openlayer-ai/openlayer-python/releases/tag/v0.22.0 🌻 |
Automated Release PR
0.22.0 (2026-04-01)
Full Changelog: v0.21.0...v0.22.0
Features
Bug Fixes
by_aliasunless set (3679fc3)Chores
Refactors
This pull request is managed by Stainless's GitHub App.
The semver version number is based on included commit messages. Alternatively, you can manually set the version number in the title of this pull request.
For a better experience, it is recommended to use either rebase-merge or squash-merge when merging this pull request.
🔗 Stainless website
📚 Read the docs
🙋 Reach out for help or questions