Compare commits

...

2 Commits

Author SHA1 Message Date
Adrian Lyjak ba2fc5c2f8 chore: set version to 0.6.62 2025-08-19 11:16:58 -04:00
Adrian Lyjak 8ed79a9d89 fix: Make env based api url overrideable 2025-08-19 11:14:47 -04:00
4 changed files with 74 additions and 27 deletions
+14 -24
View File
@@ -17,7 +17,6 @@ from llama_index.core.async_utils import asyncio_run, run_jobs
from llama_index.core.bridge.pydantic import (
Field,
PrivateAttr,
field_validator,
model_validator,
)
from llama_index.core.constants import DEFAULT_BASE_URL
@@ -105,17 +104,29 @@ class BackoffPattern(str, Enum):
EXPONENTIAL = "exponential"
def _get_default_api_key() -> str:
env_key = os.getenv("LLAMA_CLOUD_API_KEY")
if env_key is None:
raise ValueError("The API key is required.")
return env_key
def _get_default_base_url() -> str:
env_url = os.getenv("LLAMA_CLOUD_BASE_URL")
return env_url or DEFAULT_BASE_URL
class LlamaParse(BasePydanticReader):
"""A smart-parser for files."""
# Library / access specific configurations
api_key: str = Field(
default="",
default_factory=_get_default_api_key,
description="The API key for the LlamaParse API.",
validate_default=True,
)
base_url: str = Field(
default=DEFAULT_BASE_URL,
default_factory=_get_default_base_url,
description="The base URL of the Llama Parsing API.",
)
organization_id: Optional[str] = Field(
@@ -561,27 +572,6 @@ class LlamaParse(BasePydanticReader):
return data
@field_validator("api_key", mode="before", check_fields=True)
@classmethod
def validate_api_key(cls, v: str) -> str:
"""Validate the API key."""
if not v:
import os
api_key = os.getenv("LLAMA_CLOUD_API_KEY", None)
if api_key is None:
raise ValueError("The API key is required.")
return api_key
return v
@field_validator("base_url", mode="before", check_fields=True)
@classmethod
def validate_base_url(cls, v: str) -> str:
"""Validate the base URL."""
url = os.getenv("LLAMA_CLOUD_BASE_URL", None)
return url or v or DEFAULT_BASE_URL
_aclient: Union[httpx.AsyncClient, None] = PrivateAttr(default=None, init=False)
@property
+2 -2
View File
@@ -11,13 +11,13 @@ dev = [
[project]
name = "llama-parse"
version = "0.6.60"
version = "0.6.62"
description = "Parse files into RAG-Optimized formats."
authors = [{name = "Logan Markewich", email = "logan@llamaindex.ai"}]
requires-python = ">=3.9,<4.0"
readme = "README.md"
license = "MIT"
dependencies = ["llama-cloud-services>=0.6.60"]
dependencies = ["llama-cloud-services>=0.6.62"]
[project.scripts]
llama-parse = "llama_parse.cli.main:parse"
+1 -1
View File
@@ -19,7 +19,7 @@ dev = [
[project]
name = "llama-cloud-services"
version = "0.6.61"
version = "0.6.62"
description = "Tailored SDK clients for LlamaCloud services."
authors = [{name = "Logan Markewich", email = "logan@runllama.ai"}]
requires-python = ">=3.9,<4.0"
+57
View File
@@ -0,0 +1,57 @@
from collections.abc import Generator
import pytest
import os
from llama_cloud_services.parse.base import LlamaParse
@pytest.fixture(autouse=True)
def clear_env() -> Generator[None, None, None]:
"""entirely clear the environment, and then reset after test completion"""
original_env = os.environ.copy()
os.environ.clear()
try:
yield
finally:
os.environ.clear()
os.environ.update(original_env)
def test_should_obtain_api_key_base_url_from_env_vars():
os.environ["LLAMA_CLOUD_API_KEY"] = "test-api-key"
os.environ["LLAMA_CLOUD_BASE_URL"] = "https://example.test"
client = LlamaParse()
assert client.api_key == "test-api-key"
assert client.base_url == "https://example.test"
def test_should_be_able_to_pass_api_key_base_url_as_kwargs():
os.environ["LLAMA_CLOUD_API_KEY"] = "not-this-one"
os.environ["LLAMA_CLOUD_BASE_URL"] = "https://wrong.site"
client = LlamaParse(
api_key="test-api-key",
base_url="https://example.test",
)
assert client.api_key == "test-api-key"
assert client.base_url == "https://example.test"
def test_should_raise_error_if_api_key_is_not_provided():
with pytest.raises(ValueError, match="The API key is required."):
LlamaParse()
def test_should_default_to_llama_cloud_base_url_if_not_provided():
client = LlamaParse(
api_key="test-api-key",
)
assert client.base_url == "https://api.cloud.llamaindex.ai"
def test_json_parseable():
os.environ["LLAMA_CLOUD_API_KEY"] = "test-api-key"
client2 = LlamaParse.model_validate_json('{"base_url":"https://example.test"}')
assert client2.api_key == "test-api-key"
assert client2.base_url == "https://example.test"