Skip to content

The agent engineering platform.

langchain_anthropic[patch]: Invoke callback prior to yielding token (#18274)

William De Venaa37dc83a9ea7
diff --git a/libs/partners/anthropic/langchain_anthropic/llms.py b/libs/partners/anthropic/langchain_anthropic/llms.py
index d5d04a7f3e..82f1b80433 100644
--- a/libs/partners/anthropic/langchain_anthropic/llms.py
+++ b/libs/partners/anthropic/langchain_anthropic/llms.py
@@ -300,9 +300,10 @@ class AnthropicLLM(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,
@@ -336,9 +337,10 @@ class AnthropicLLM(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."""