Skip to content

The agent engineering platform.

community[patch]: Invoke callback prior to yielding token (#18452)

William De Venaeb04d0d3e20b
diff --git a/libs/community/langchain_community/llms/anthropic.py b/libs/community/langchain_community/llms/anthropic.py
index f5674a0766..ad37bd9e4a 100644
--- a/libs/community/langchain_community/llms/anthropic.py
+++ b/libs/community/langchain_community/llms/anthropic.py
@@ -309,9 +309,9 @@ class Anthropic(LLM, _AnthropicCommon):
prompt=self._wrap_prompt(prompt), stop_sequences=stop, stream=True, **params
):
chunk = GenerationChunk(text=token.completion)
- yield chunk
if run_manager:
run_manager.on_llm_new_token(chunk.text, chunk=chunk)
+ yield chunk
async def _astream(
self,
@@ -345,9 +345,9 @@ class Anthropic(LLM, _AnthropicCommon):
**params,
):
chunk = GenerationChunk(text=token.completion)
- yield chunk
if run_manager:
await run_manager.on_llm_new_token(chunk.text, chunk=chunk)
+ yield chunk
def get_num_tokens(self, text: str) -> int:
"""Calculate number of tokens."""