diff --git a/libs/core/langchain_core/language_models/base.py b/libs/core/langchain_core/language_models/base.py index 5e60c890a47..ab3fbd03015 100644 --- a/libs/core/langchain_core/language_models/base.py +++ b/libs/core/langchain_core/language_models/base.py @@ -375,7 +375,7 @@ class BaseLanguageModel( Returns: The sum of the number of tokens across the messages. """ - return sum([self.get_num_tokens(get_buffer_string([m])) for m in messages]) + return sum(self.get_num_tokens(get_buffer_string([m])) for m in messages) @classmethod def _all_required_field_names(cls) -> set: diff --git a/libs/core/langchain_core/language_models/llms.py b/libs/core/langchain_core/language_models/llms.py index 7fd47e627d7..302a50aa095 100644 --- a/libs/core/langchain_core/language_models/llms.py +++ b/libs/core/langchain_core/language_models/llms.py @@ -1387,10 +1387,10 @@ class BaseLLM(BaseLanguageModel[str], ABC): prompt_dict = self.dict() if save_path.suffix == ".json": - with open(file_path, "w") as f: + with open(file_path, "w", encoding="utf-8") as f: json.dump(prompt_dict, f, indent=4) elif save_path.suffix.endswith((".yaml", ".yml")): - with open(file_path, "w") as f: + with open(file_path, "w", encoding="utf-8") as f: yaml.dump(prompt_dict, f, default_flow_style=False) else: msg = f"{save_path} must be json or yaml" diff --git a/libs/core/langchain_core/prompts/base.py b/libs/core/langchain_core/prompts/base.py index e399974a5b0..3331e4783cd 100644 --- a/libs/core/langchain_core/prompts/base.py +++ b/libs/core/langchain_core/prompts/base.py @@ -359,10 +359,10 @@ class BasePromptTemplate( directory_path.mkdir(parents=True, exist_ok=True) if save_path.suffix == ".json": - with open(file_path, "w") as f: + with open(file_path, "w", encoding="utf-8") as f: json.dump(prompt_dict, f, indent=4) elif save_path.suffix.endswith((".yaml", ".yml")): - with open(file_path, "w") as f: + with open(file_path, "w", encoding="utf-8") as f: yaml.dump(prompt_dict, f, default_flow_style=False) else: msg = f"{save_path} must be json or yaml" diff --git a/libs/core/langchain_core/prompts/chat.py b/libs/core/langchain_core/prompts/chat.py index 111aed89b3c..ab52c0d62d2 100644 --- a/libs/core/langchain_core/prompts/chat.py +++ b/libs/core/langchain_core/prompts/chat.py @@ -588,7 +588,7 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate): Returns: A new instance of this class. """ - with open(str(template_file)) as f: + with open(str(template_file), encoding="utf-8") as f: template = f.read() return cls.from_template(template, input_variables=input_variables, **kwargs) diff --git a/libs/core/langchain_core/prompts/loading.py b/libs/core/langchain_core/prompts/loading.py index 3d65f8323f1..4c67b5486fe 100644 --- a/libs/core/langchain_core/prompts/loading.py +++ b/libs/core/langchain_core/prompts/loading.py @@ -53,7 +53,7 @@ def _load_template(var_name: str, config: dict) -> dict: template_path = Path(config.pop(f"{var_name}_path")) # Load the template. if template_path.suffix == ".txt": - with open(template_path) as f: + with open(template_path, encoding="utf-8") as f: template = f.read() else: raise ValueError @@ -67,7 +67,7 @@ def _load_examples(config: dict) -> dict: if isinstance(config["examples"], list): pass elif isinstance(config["examples"], str): - with open(config["examples"]) as f: + with open(config["examples"], encoding="utf-8") as f: if config["examples"].endswith(".json"): examples = json.load(f) elif config["examples"].endswith((".yaml", ".yml")): diff --git a/libs/core/langchain_core/rate_limiters.py b/libs/core/langchain_core/rate_limiters.py index 043e54cc38f..79bd054bc3a 100644 --- a/libs/core/langchain_core/rate_limiters.py +++ b/libs/core/langchain_core/rate_limiters.py @@ -248,7 +248,7 @@ class InMemoryRateLimiter(BaseRateLimiter): if not blocking: return self._consume() - while not self._consume(): + while not self._consume(): # noqa: ASYNC110 await asyncio.sleep(self.check_every_n_seconds) return True diff --git a/libs/core/tests/unit_tests/runnables/test_tracing_interops.py b/libs/core/tests/unit_tests/runnables/test_tracing_interops.py index af6a5e84ce9..60683d33fc7 100644 --- a/libs/core/tests/unit_tests/runnables/test_tracing_interops.py +++ b/libs/core/tests/unit_tests/runnables/test_tracing_interops.py @@ -314,7 +314,7 @@ async def test_runnable_sequence_parallel_trace_nesting(method: str) -> None: "other_thing": "RunnableParallel", "after": "RunnableSequence", } - assert len(posts) == sum([1 if isinstance(n, str) else len(n) for n in name_order]) + assert len(posts) == sum(1 if isinstance(n, str) else len(n) for n in name_order) prev_dotted_order = None dotted_order_map = {} id_map = {}