Skip to content

Commit 37393b7

Browse files
authored
fix: Make env based api url overrideable (#881)
1 parent ecd859a commit 37393b7

File tree

4 files changed

+74
-27
lines changed

4 files changed

+74
-27
lines changed

py/llama_cloud_services/parse/base.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from llama_index.core.bridge.pydantic import (
1818
Field,
1919
PrivateAttr,
20-
field_validator,
2120
model_validator,
2221
)
2322
from llama_index.core.constants import DEFAULT_BASE_URL
@@ -105,17 +104,29 @@ class BackoffPattern(str, Enum):
105104
EXPONENTIAL = "exponential"
106105

107106

107+
def _get_default_api_key() -> str:
108+
env_key = os.getenv("LLAMA_CLOUD_API_KEY")
109+
if env_key is None:
110+
raise ValueError("The API key is required.")
111+
return env_key
112+
113+
114+
def _get_default_base_url() -> str:
115+
env_url = os.getenv("LLAMA_CLOUD_BASE_URL")
116+
return env_url or DEFAULT_BASE_URL
117+
118+
108119
class LlamaParse(BasePydanticReader):
109120
"""A smart-parser for files."""
110121

111122
# Library / access specific configurations
112123
api_key: str = Field(
113-
default="",
124+
default_factory=_get_default_api_key,
114125
description="The API key for the LlamaParse API.",
115126
validate_default=True,
116127
)
117128
base_url: str = Field(
118-
default=DEFAULT_BASE_URL,
129+
default_factory=_get_default_base_url,
119130
description="The base URL of the Llama Parsing API.",
120131
)
121132
organization_id: Optional[str] = Field(
@@ -561,27 +572,6 @@ def warn_extra_params(cls, data: Dict[str, Any]) -> Dict[str, Any]:
561572

562573
return data
563574

564-
@field_validator("api_key", mode="before", check_fields=True)
565-
@classmethod
566-
def validate_api_key(cls, v: str) -> str:
567-
"""Validate the API key."""
568-
if not v:
569-
import os
570-
571-
api_key = os.getenv("LLAMA_CLOUD_API_KEY", None)
572-
if api_key is None:
573-
raise ValueError("The API key is required.")
574-
return api_key
575-
576-
return v
577-
578-
@field_validator("base_url", mode="before", check_fields=True)
579-
@classmethod
580-
def validate_base_url(cls, v: str) -> str:
581-
"""Validate the base URL."""
582-
url = os.getenv("LLAMA_CLOUD_BASE_URL", None)
583-
return url or v or DEFAULT_BASE_URL
584-
585575
_aclient: Union[httpx.AsyncClient, None] = PrivateAttr(default=None, init=False)
586576

587577
@property

py/llama_parse/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ dev = [
1111

1212
[project]
1313
name = "llama-parse"
14-
version = "0.6.60"
14+
version = "0.6.62"
1515
description = "Parse files into RAG-Optimized formats."
1616
authors = [{name = "Logan Markewich", email = "[email protected]"}]
1717
requires-python = ">=3.9,<4.0"
1818
readme = "README.md"
1919
license = "MIT"
20-
dependencies = ["llama-cloud-services>=0.6.60"]
20+
dependencies = ["llama-cloud-services>=0.6.62"]
2121

2222
[project.scripts]
2323
llama-parse = "llama_parse.cli.main:parse"

py/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dev = [
1919

2020
[project]
2121
name = "llama-cloud-services"
22-
version = "0.6.61"
22+
version = "0.6.62"
2323
description = "Tailored SDK clients for LlamaCloud services."
2424
authors = [{name = "Logan Markewich", email = "[email protected]"}]
2525
requires-python = ">=3.9,<4.0"

py/unit_tests/parse/test_base.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from collections.abc import Generator
2+
import pytest
3+
import os
4+
5+
from llama_cloud_services.parse.base import LlamaParse
6+
7+
8+
@pytest.fixture(autouse=True)
9+
def clear_env() -> Generator[None, None, None]:
10+
"""entirely clear the environment, and then reset after test completion"""
11+
original_env = os.environ.copy()
12+
os.environ.clear()
13+
try:
14+
yield
15+
finally:
16+
os.environ.clear()
17+
os.environ.update(original_env)
18+
19+
20+
def test_should_obtain_api_key_base_url_from_env_vars():
21+
os.environ["LLAMA_CLOUD_API_KEY"] = "test-api-key"
22+
os.environ["LLAMA_CLOUD_BASE_URL"] = "https://example.test"
23+
24+
client = LlamaParse()
25+
assert client.api_key == "test-api-key"
26+
assert client.base_url == "https://example.test"
27+
28+
29+
def test_should_be_able_to_pass_api_key_base_url_as_kwargs():
30+
os.environ["LLAMA_CLOUD_API_KEY"] = "not-this-one"
31+
os.environ["LLAMA_CLOUD_BASE_URL"] = "https://wrong.site"
32+
33+
client = LlamaParse(
34+
api_key="test-api-key",
35+
base_url="https://example.test",
36+
)
37+
assert client.api_key == "test-api-key"
38+
assert client.base_url == "https://example.test"
39+
40+
41+
def test_should_raise_error_if_api_key_is_not_provided():
42+
with pytest.raises(ValueError, match="The API key is required."):
43+
LlamaParse()
44+
45+
46+
def test_should_default_to_llama_cloud_base_url_if_not_provided():
47+
client = LlamaParse(
48+
api_key="test-api-key",
49+
)
50+
assert client.base_url == "https://api.cloud.llamaindex.ai"
51+
52+
53+
def test_json_parseable():
54+
os.environ["LLAMA_CLOUD_API_KEY"] = "test-api-key"
55+
client2 = LlamaParse.model_validate_json('{"base_url":"https://example.test"}')
56+
assert client2.api_key == "test-api-key"
57+
assert client2.base_url == "https://example.test"

0 commit comments

Comments
 (0)