Skip to content

Commit e092529

Browse files
Move create_vellum_client to vellum.utils and reexport (#3133)
* Move create_vellum_client to vellum.utils and reexport from vellum.workflows.vellum_client - Moved create_vellum_client, create_vellum_environment, and _resolve_env helper functions to src/vellum/utils/vellum_client.py - Updated src/vellum/workflows/vellum_client.py to reexport these functions for backward compatibility - Also reexport Vellum class to maintain existing imports - Updated src/vellum/utils/__init__.py to export create_vellum_client Co-Authored-By: [email protected] <[email protected]> * Fix test mocking to patch correct path after refactoring Updated conftest.py to patch vellum.utils.vellum_client.Vellum (where create_vellum_client now imports from) in addition to vellum.workflows.vellum_client.Vellum for backward compatibility. This ensures test mocks work correctly after moving create_vellum_client to vellum.utils. Co-Authored-By: [email protected] <[email protected]> * Remove unnecessary export from vellum.utils.__init__.py The original request was to reexport from vellum.workflows.vellum_client, not from vellum.utils. Also, other utils modules (typing, uuid, json_encoder) don't export from __init__.py, so we should follow that pattern and keep the utils __init__.py empty. Co-Authored-By: [email protected] <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 36c2709 commit e092529

File tree

3 files changed

+45
-40
lines changed

3 files changed

+45
-40
lines changed

conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def _generate_uuid() -> UUID:
8282

8383
@pytest.fixture
8484
def vellum_client_class(mocker: MockerFixture) -> Any:
85-
vellum_client_class = mocker.patch("vellum.workflows.vellum_client.Vellum")
85+
vellum_client_class = mocker.patch("vellum.utils.vellum_client.Vellum")
86+
mocker.patch("vellum.workflows.vellum_client.Vellum", vellum_client_class)
8687
return vellum_client_class
8788

8889

src/vellum/utils/vellum_client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import os
2+
from typing import List, Optional
3+
4+
from vellum import Vellum, VellumEnvironment
5+
from vellum.client.types.api_version_enum import ApiVersionEnum
6+
7+
8+
def create_vellum_client(
9+
api_key: Optional[str] = None,
10+
api_url: Optional[str] = None,
11+
api_version: Optional[ApiVersionEnum] = None,
12+
) -> Vellum:
13+
if api_key is None:
14+
api_key = os.getenv("VELLUM_API_KEY", default="")
15+
16+
return Vellum(
17+
api_key=api_key,
18+
environment=create_vellum_environment(api_url),
19+
api_version=api_version,
20+
)
21+
22+
23+
def create_vellum_environment(api_url: Optional[str] = None) -> VellumEnvironment:
24+
return VellumEnvironment(
25+
default=_resolve_env([api_url, "VELLUM_DEFAULT_API_URL", "VELLUM_API_URL"], "https://api.vellum.ai"),
26+
documents=_resolve_env([api_url, "VELLUM_DOCUMENTS_API_URL", "VELLUM_API_URL"], "https://documents.vellum.ai"),
27+
predict=_resolve_env([api_url, "VELLUM_PREDICT_API_URL", "VELLUM_API_URL"], "https://predict.vellum.ai"),
28+
)
29+
30+
31+
def _resolve_env(names: List[Optional[str]], default: str = "") -> str:
32+
for name in names:
33+
if not name:
34+
continue
35+
36+
value = os.getenv(name)
37+
if value:
38+
return value
39+
40+
return default
Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,4 @@
1-
import os
2-
from typing import List, Optional
1+
from vellum import Vellum
2+
from vellum.utils.vellum_client import create_vellum_client, create_vellum_environment
33

4-
from vellum import Vellum, VellumEnvironment
5-
from vellum.client.types.api_version_enum import ApiVersionEnum
6-
7-
8-
def create_vellum_client(
9-
api_key: Optional[str] = None,
10-
api_url: Optional[str] = None,
11-
api_version: Optional[ApiVersionEnum] = None,
12-
) -> Vellum:
13-
if api_key is None:
14-
api_key = os.getenv("VELLUM_API_KEY", default="")
15-
16-
return Vellum(
17-
api_key=api_key,
18-
environment=create_vellum_environment(api_url),
19-
api_version=api_version,
20-
)
21-
22-
23-
def create_vellum_environment(api_url: Optional[str] = None) -> VellumEnvironment:
24-
return VellumEnvironment(
25-
default=_resolve_env([api_url, "VELLUM_DEFAULT_API_URL", "VELLUM_API_URL"], "https://api.vellum.ai"),
26-
documents=_resolve_env([api_url, "VELLUM_DOCUMENTS_API_URL", "VELLUM_API_URL"], "https://documents.vellum.ai"),
27-
predict=_resolve_env([api_url, "VELLUM_PREDICT_API_URL", "VELLUM_API_URL"], "https://predict.vellum.ai"),
28-
)
29-
30-
31-
def _resolve_env(names: List[Optional[str]], default: str = "") -> str:
32-
for name in names:
33-
if not name:
34-
continue
35-
36-
value = os.getenv(name)
37-
if value:
38-
return value
39-
40-
return default
4+
__all__ = ["Vellum", "create_vellum_client", "create_vellum_environment"]

0 commit comments

Comments
 (0)