Skip to content

Commit

Permalink
Create a module for pydantic v1 (langchain-ai#187)
Browse files Browse the repository at this point in the history
Create a module for pydantic v1 code, so we don't have to worry about
conditional imports or try/except blocks in the codebase.
  • Loading branch information
eyurtsev authored Nov 9, 2023
1 parent 5b46eab commit e188f2e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
5 changes: 1 addition & 4 deletions langserve/playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
from fastapi.responses import Response
from langchain.schema.runnable import Runnable

try:
from pydantic.v1 import BaseModel
except ImportError:
from pydantic import BaseModel
from langserve.pydantic_v1 import BaseModel


class PlaygroundTemplate(Template):
Expand Down
10 changes: 0 additions & 10 deletions langserve/pydantic.py

This file was deleted.

25 changes: 25 additions & 0 deletions langserve/pydantic_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from importlib import metadata

## Create namespaces for pydantic v1 and v2.
# This code must stay at the top of the file before other modules may
# attempt to import pydantic since it adds pydantic_v1 and pydantic_v2 to sys.modules.
#
# This hack is done for the following reasons:
# * Langchain will attempt to remain compatible with both pydantic v1 and v2 since
# both dependencies and dependents may be stuck on either version of v1 or v2.
# * Creating namespaces for pydantic v1 and v2 should allow us to write code that
# unambiguously uses either v1 or v2 API.
# * This change is easier to roll out and roll back.

try:
# F401: imported but unused
from pydantic.v1 import BaseModel, Field, ValidationError # noqa: F401
except ImportError:
from pydantic import BaseModel, Field, ValidationError # noqa: F401


# This is not a pydantic v1 thing, but it feels too small to create a new module for.
try:
_PYDANTIC_MAJOR_VERSION: int = int(metadata.version("pydantic").split(".")[0])
except metadata.PackageNotFoundError:
_PYDANTIC_MAJOR_VERSION = -1
8 changes: 2 additions & 6 deletions langserve/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
from typing import Dict, List, Optional, Union
from uuid import UUID

from langserve.pydantic import PYDANTIC_MAJOR_VERSION
from pydantic import BaseModel # Floats between v1 and v2

if PYDANTIC_MAJOR_VERSION == 2:
from pydantic.v1 import BaseModel as BaseModelV1
else:
from pydantic import BaseModel as BaseModelV1
from pydantic import BaseModel
from langserve.pydantic_v1 import BaseModel as BaseModelV1


class CustomUserType(BaseModelV1):
Expand Down
7 changes: 1 addition & 6 deletions langserve/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@
LLMResult,
)

from langserve.pydantic_v1 import BaseModel, ValidationError
from langserve.validation import CallbackEvent

try:
from pydantic.v1 import BaseModel, ValidationError
except ImportError:
from pydantic import BaseModel, ValidationError


logger = logging.getLogger(__name__)


Expand Down
6 changes: 3 additions & 3 deletions langserve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from langserve.callbacks import AsyncEventAggregatorCallback, CallbackEventDict
from langserve.lzstring import LZString
from langserve.playground import serve_playground
from langserve.pydantic import PYDANTIC_MAJOR_VERSION
from langserve.pydantic_v1 import _PYDANTIC_MAJOR_VERSION
from langserve.schema import (
BatchResponseMetadata,
CustomUserType,
Expand Down Expand Up @@ -1045,8 +1045,8 @@ async def feedback(feedback_create_req: FeedbackCreateRequest) -> Feedback:
#######################################
# Documentation variants of end points.
#######################################
# At the moment, we only support pydantic 1.x
if PYDANTIC_MAJOR_VERSION == 1:
# At the moment, we only support pydantic 1.x for documentation
if _PYDANTIC_MAJOR_VERSION == 1:

@app.post(
namespace + "/c/{config_hash}/invoke",
Expand Down

0 comments on commit e188f2e

Please sign in to comment.