From 0486404a7409beb3bcbd3cd5f86e861b2cf9ee46 Mon Sep 17 00:00:00 2001 From: William De Vena <60664495+williamdevena@users.noreply.github.com> Date: Thu, 29 Feb 2024 01:00:08 +0100 Subject: [PATCH] langchain_openai[patch]: Invoke callback prior to yielding token (#18269) ## PR title langchain_openai[patch]: Invoke callback prior to yielding token ## PR message Description: Invoke callback prior to yielding token in _stream and _astream methods for langchain_openai. Issue: https://github.com/langchain-ai/langchain/issues/16913 Dependencies: None Twitter handle: None --- libs/partners/openai/langchain_openai/llms/base.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/partners/openai/langchain_openai/llms/base.py b/libs/partners/openai/langchain_openai/llms/base.py index ec8ff643fa2..4b3b7d56d22 100644 --- a/libs/partners/openai/langchain_openai/llms/base.py +++ b/libs/partners/openai/langchain_openai/llms/base.py @@ -253,7 +253,7 @@ class BaseOpenAI(BaseLLM): if not isinstance(stream_resp, dict): stream_resp = stream_resp.model_dump() chunk = _stream_response_to_generation_chunk(stream_resp) - yield chunk + if run_manager: run_manager.on_llm_new_token( chunk.text, @@ -265,6 +265,7 @@ class BaseOpenAI(BaseLLM): else None ), ) + yield chunk async def _astream( self, @@ -281,7 +282,7 @@ class BaseOpenAI(BaseLLM): if not isinstance(stream_resp, dict): stream_resp = stream_resp.model_dump() chunk = _stream_response_to_generation_chunk(stream_resp) - yield chunk + if run_manager: await run_manager.on_llm_new_token( chunk.text, @@ -293,6 +294,7 @@ class BaseOpenAI(BaseLLM): else None ), ) + yield chunk def _generate( self,