Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when mounting same chain twice in same app #134

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions langserve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,16 @@ def _unpack_input(validated_model: BaseModel) -> Any:
return model


def _rename_pydantic_model(model: Type[BaseModel], name: str) -> Type[BaseModel]:
def _rename_pydantic_model(model: Type[BaseModel], prefix: str) -> Type[BaseModel]:
"""Rename the given pydantic model to the given name."""
return create_model(
name,
prefix + model.__name__,
__config__=model.__config__,
**{
fieldname: (
field.annotation,
_rename_pydantic_model(field.annotation, prefix)
if isclass(field.annotation) and issubclass(field.annotation, BaseModel)
else field.annotation,
Field(
field.default,
title=fieldname,
Expand Down Expand Up @@ -155,7 +157,7 @@ def _resolve_model(
if model.__name__ in _SEEN_NAMES and hash_ not in _MODEL_REGISTRY:
# If the model name has been seen before, but the model itself is different
# generate a new name for the model.
model_to_use = _rename_pydantic_model(model, f"{namespace}{model.__name__}")
model_to_use = _rename_pydantic_model(model, namespace)
hash_ = model_to_use.schema_json()
else:
model_to_use = model
Expand All @@ -180,10 +182,7 @@ def _add_namespace_to_model(namespace: str, model: Type[BaseModel]) -> Type[Base
Returns:
A new model with name prepended with the given namespace.
"""
model_with_unique_name = _rename_pydantic_model(
model,
f"{namespace}{model.__name__}",
)
model_with_unique_name = _rename_pydantic_model(model, namespace)
model_with_unique_name.update_forward_refs()
return model_with_unique_name

Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_server_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ class Foo(BaseModel):
Model = _rename_pydantic_model(Foo, "Bar")

assert isinstance(Model, type)
assert Model.__name__ == "Bar"
assert Model.__name__ == "BarFoo"


@pytest.mark.asyncio
Expand Down