Skip to content

Commit c3d05ce

Browse files
committed
fix(client): some minor code fixes...
1 parent 84420c6 commit c3d05ce

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

langfuse/api/core/jsonable_encoder.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ def jsonable_encoder(
9898
try:
9999
data = dict(obj)
100100
except Exception as e:
101-
errors: List[Exception] = []
102-
errors.append(e)
101+
errors: List[Exception] = [e]
103102
try:
104103
data = vars(obj)
105104
except Exception as e:

langfuse/client.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ def create_prompt(
12981298
name: str,
12991299
prompt: List[ChatMessageDict],
13001300
is_active: Optional[bool] = None, # deprecated
1301-
labels: List[str] = [],
1301+
labels: Optional[List[str]] = None,
13021302
tags: Optional[List[str]] = None,
13031303
type: Optional[Literal["chat"]],
13041304
config: Optional[Any] = None,
@@ -1312,7 +1312,7 @@ def create_prompt(
13121312
name: str,
13131313
prompt: str,
13141314
is_active: Optional[bool] = None, # deprecated
1315-
labels: List[str] = [],
1315+
labels: Optional[List[str]] = None,
13161316
tags: Optional[List[str]] = None,
13171317
type: Optional[Literal["text"]] = "text",
13181318
config: Optional[Any] = None,
@@ -1325,7 +1325,7 @@ def create_prompt(
13251325
name: str,
13261326
prompt: Union[str, List[ChatMessageDict]],
13271327
is_active: Optional[bool] = None, # deprecated
1328-
labels: List[str] = [],
1328+
labels: Optional[List[str]] = None,
13291329
tags: Optional[List[str]] = None,
13301330
type: Optional[Literal["chat", "text"]] = "text",
13311331
config: Optional[Any] = None,
@@ -1347,6 +1347,8 @@ def create_prompt(
13471347
TextPromptClient: The prompt if type argument is 'text'.
13481348
ChatPromptClient: The prompt if type argument is 'chat'.
13491349
"""
1350+
if labels is None:
1351+
labels = []
13501352
try:
13511353
self.log.debug(f"Creating prompt {name=}, {version=}, {labels=}")
13521354

@@ -1401,7 +1403,7 @@ def update_prompt(
14011403
*,
14021404
name: str,
14031405
version: int,
1404-
new_labels: List[str] = [],
1406+
new_labels: Optional[List[str]] = None,
14051407
):
14061408
"""Update an existing prompt version in Langfuse. The Langfuse SDK prompt cache is invalidated for all prompts witht he specified name.
14071409
@@ -1414,6 +1416,8 @@ def update_prompt(
14141416
Prompt: The updated prompt from the Langfuse API.
14151417
14161418
"""
1419+
if new_labels is None:
1420+
new_labels = []
14171421
updated_prompt = self.client.prompt_version.update(
14181422
name=name,
14191423
version=version,
@@ -3424,7 +3428,7 @@ def observe_llama_index(
34243428
run_name: str,
34253429
run_description: Optional[str] = None,
34263430
run_metadata: Optional[Any] = None,
3427-
llama_index_integration_constructor_kwargs: Optional[Dict[str, Any]] = {},
3431+
llama_index_integration_constructor_kwargs: Optional[Dict[str, Any]] = None,
34283432
):
34293433
"""Context manager for observing LlamaIndex operations linked to this dataset item.
34303434
@@ -3454,6 +3458,8 @@ def observe_llama_index(
34543458
Raises:
34553459
ImportError: If required modules for LlamaIndex integration are not available.
34563460
"""
3461+
if llama_index_integration_constructor_kwargs is None:
3462+
llama_index_integration_constructor_kwargs = {}
34573463
metadata = {
34583464
"dataset_item_id": self.id,
34593465
"run_name": run_name,
@@ -3513,7 +3519,7 @@ def get_llama_index_handler(
35133519
run_name: str,
35143520
run_description: Optional[str] = None,
35153521
run_metadata: Optional[Any] = None,
3516-
llama_index_integration_constructor_kwargs: Optional[Dict[str, Any]] = {},
3522+
llama_index_integration_constructor_kwargs: Optional[Dict[str, Any]] = None,
35173523
):
35183524
"""Create and get a llama-index callback handler linked to this dataset item.
35193525
@@ -3526,6 +3532,8 @@ def get_llama_index_handler(
35263532
Returns:
35273533
LlamaIndexCallbackHandler: An instance of LlamaIndexCallbackHandler linked to the dataset item.
35283534
"""
3535+
if llama_index_integration_constructor_kwargs is None:
3536+
llama_index_integration_constructor_kwargs = {}
35293537
metadata = {
35303538
"dataset_item_id": self.id,
35313539
"run_name": run_name,

langfuse/decorators/langfuse_decorator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,12 @@ def _prepare_call(
289289
capture_input: bool,
290290
is_method: bool = False,
291291
func_args: Tuple = (),
292-
func_kwargs: Dict = {},
292+
func_kwargs: Optional[Dict] = None,
293293
) -> Optional[
294294
Union[StatefulSpanClient, StatefulTraceClient, StatefulGenerationClient]
295295
]:
296+
if func_kwargs is None:
297+
func_kwargs = {}
296298
try:
297299
stack = _observation_stack_context.get().copy()
298300
parent = stack[-1] if stack else None
@@ -392,8 +394,10 @@ def _get_input_from_func_args(
392394
*,
393395
is_method: bool = False,
394396
func_args: Tuple = (),
395-
func_kwargs: Dict = {},
397+
func_kwargs: Optional[Dict] = None,
396398
) -> Any:
399+
if func_kwargs is None:
400+
func_kwargs = {}
397401
# Remove implicitly passed "self" or "cls" argument for instance or class methods
398402
logged_args = func_args[1:] if is_method else func_args
399403
raw_input = {

langfuse/openai.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ def initialize(self):
847847

848848
return self._langfuse
849849

850+
@classmethod
850851
def flush(cls):
851852
cls._langfuse.flush()
852853

langfuse/parse_error.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
}
4646

4747

48-
def generate_error_message_fern(error: Error) -> str:
48+
def generate_error_message_fern(error: Exception) -> str:
4949
if isinstance(error, AccessDeniedError):
5050
return errorResponseByCode.get(403, defaultErrorResponse)
5151
elif isinstance(error, MethodNotAllowedError):
@@ -67,7 +67,7 @@ def generate_error_message_fern(error: Error) -> str:
6767
return defaultErrorResponse
6868

6969

70-
def handle_fern_exception(exception: Error) -> None:
70+
def handle_fern_exception(exception: Exception) -> None:
7171
log = logging.getLogger("langfuse")
7272
log.debug(exception)
7373
error_message = generate_error_message_fern(exception)

langfuse/utils/error_logging.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ def wrapper(*args, **kwargs):
1818
return wrapper
1919

2020

21-
def auto_decorate_methods_with(decorator, exclude: Optional[List[str]] = []):
21+
def auto_decorate_methods_with(decorator, exclude: Optional[List[str]] = None):
2222
"""Class decorator to automatically apply a given decorator to all
2323
methods of a class.
2424
"""
2525

26+
if exclude is None:
27+
exclude = []
28+
2629
def class_decorator(cls):
2730
for attr_name, attr_value in cls.__dict__.items():
2831
if attr_name in exclude:

0 commit comments

Comments
 (0)