22
33import re
44from abc import ABC , abstractmethod
5- from typing import Any , Dict , List , Literal , Optional , Tuple , TypedDict , Union
5+ from typing import Any , Dict , List , Literal , Optional , Sequence , Tuple , TypedDict , Union
66from langfuse .logger import langfuse_logger
77
88from langfuse .api .resources .commons .types .dataset import (
@@ -161,7 +161,12 @@ def __init__(self, prompt: Prompt, is_fallback: bool = False):
161161 self .is_fallback = is_fallback
162162
163163 @abstractmethod
164- def compile (self , ** kwargs : Any ) -> Union [str , List [Dict [str , Any ]]]:
164+ def compile (
165+ self , ** kwargs : Union [str , Any ]
166+ ) -> Union [
167+ str ,
168+ Sequence [Union [ChatMessageDict , ChatMessageWithPlaceholdersDict_Placeholder ]],
169+ ]:
165170 pass
166171
167172 @property
@@ -255,7 +260,7 @@ def __init__(self, prompt: Prompt_Text, is_fallback: bool = False):
255260 super ().__init__ (prompt , is_fallback )
256261 self .prompt = prompt .prompt
257262
258- def compile (self , ** kwargs : Any ) -> str :
263+ def compile (self , ** kwargs : Union [ str , Any ] ) -> str :
259264 return TemplateParser .compile_template (self .prompt , kwargs )
260265
261266 @property
@@ -274,7 +279,7 @@ def __eq__(self, other: object) -> bool:
274279
275280 return False
276281
277- def get_langchain_prompt (self , ** kwargs : Any ) -> str :
282+ def get_langchain_prompt (self , ** kwargs : Union [ str , Any ] ) -> str :
278283 """Convert Langfuse prompt into string compatible with Langchain PromptTemplate.
279284
280285 This method adapts the mustache-style double curly braces {{variable}} used in Langfuse
@@ -314,12 +319,15 @@ def __init__(self, prompt: Prompt_Chat, is_fallback: bool = False):
314319 self .prompt .append (
315320 ChatMessageWithPlaceholdersDict_Message (
316321 type = "message" ,
317- role = p .role ,
318- content = p .content ,
322+ role = p .role , # type: ignore
323+ content = p .content , # type: ignore
319324 ),
320325 )
321326
322- def compile (self , ** kwargs : Any ) -> List [Dict [str , Any ]]:
327+ def compile (
328+ self ,
329+ ** kwargs : Union [str , Any ],
330+ ) -> Sequence [Union [ChatMessageDict , ChatMessageWithPlaceholdersDict_Placeholder ]]:
323331 """Compile the prompt with placeholders and variables.
324332
325333 Args:
@@ -358,9 +366,9 @@ def compile(self, **kwargs: Any) -> List[Dict[str, Any]]:
358366 ):
359367 compiled_messages .append (
360368 {
361- "role" : msg ["role" ],
369+ "role" : msg ["role" ], # type: ignore
362370 "content" : TemplateParser .compile_template (
363- msg ["content" ],
371+ msg ["content" ], # type: ignore
364372 kwargs ,
365373 ),
366374 },
@@ -392,7 +400,7 @@ def compile(self, **kwargs: Any) -> List[Dict[str, Any]]:
392400 unresolved_placeholders_message = f"Placeholders { unresolved_placeholders } have not been resolved. Pass them as keyword arguments to compile()."
393401 langfuse_logger .warning (unresolved_placeholders_message )
394402
395- return compiled_messages
403+ return compiled_messages # type: ignore
396404
397405 @property
398406 def variables (self ) -> List [str ]:
@@ -434,7 +442,9 @@ def __eq__(self, other: object) -> bool:
434442
435443 return False
436444
437- def get_langchain_prompt (self , ** kwargs : Any ) -> List [Union [Tuple [str , str ], Any ]]:
445+ def get_langchain_prompt (
446+ self , ** kwargs : Union [str , Any ]
447+ ) -> List [Union [Tuple [str , str ], Any ]]:
438448 """Convert Langfuse prompt into string compatible with Langchain ChatPromptTemplate.
439449
440450 It specifically adapts the mustache-style double curly braces {{variable}} used in Langfuse
@@ -455,9 +465,9 @@ def get_langchain_prompt(self, **kwargs: Any) -> List[Union[Tuple[str, str], Any
455465 langchain_messages : List [Union [Tuple [str , str ], Any ]] = []
456466
457467 for msg in compiled_messages :
458- if isinstance (msg , dict ) and "type" in msg and msg ["type" ] == "placeholder" :
468+ if isinstance (msg , dict ) and "type" in msg and msg ["type" ] == "placeholder" : # type: ignore
459469 # unresolved placeholder -> add LC MessagesPlaceholder
460- placeholder_name = msg ["name" ]
470+ placeholder_name = msg ["name" ] # type: ignore
461471 try :
462472 from langchain_core .prompts .chat import MessagesPlaceholder # noqa: PLC0415, I001
463473
@@ -471,8 +481,8 @@ def get_langchain_prompt(self, **kwargs: Any) -> List[Union[Tuple[str, str], Any
471481 if isinstance (msg , dict ) and "role" in msg and "content" in msg :
472482 langchain_messages .append (
473483 (
474- msg ["role" ],
475- self ._get_langchain_prompt_string (msg ["content" ]),
484+ msg ["role" ], # type: ignore
485+ self ._get_langchain_prompt_string (msg ["content" ]), # type: ignore
476486 ),
477487 )
478488
0 commit comments