Skip to content

[BUG]: LangChain expects at least one chunk from a streaming trace after timeout #14688

@MartinGotelli

Description

@MartinGotelli

Tracer Version(s)

3.14.2

Python Version(s)

3.11.4

Pip Version(s)

pip 24.3.1

Bug Report

Using ddtrace-run command with LangChain and ChatOpenAI, after a timeout, the ddtrace integration fails.

packages/ddtrace/contrib/internal/langchain/patch.py", line 421, in _on_span_finished
    joined_chunks = streamed_chunks[0]
                    ~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

Reproduction Code

ddtrace-run python my_script.py -> "Oops!"
python my_script.py -> "Success!"

from langchain_openai import ChatOpenAI
from openai import APITimeoutError


def run():
    chain = ChatOpenAI(
        api_key="my_key",
        timeout=0.0001,
    )
    result = chain.stream("Hello, my name is")
    try:
        next(result)
    except APITimeoutError as e:
        print(f"Success! Caught expected timeout error: {e}")
    except Exception as e:
        print(f"Oops! Caught unexpected error: {e}")


if __name__ == "__main__":
    run()

Error Logs

No response

Libraries in Use

langchain==0.3.24
langchain-aws==0.2.10
langchain-community==0.3.22
langchain-core==0.3.55
langchain-openai==0.3.10
langchain-postgres==0.0.13
langchain-text-splitters==0.3.8
langdetect==1.0.9
langgraph==0.2.61
langgraph-checkpoint==2.0.9
langgraph-sdk==0.1.51
langsmith==0.2.10
openai==1.68.2

Operating System

No response

Metadata

Metadata

Assignees

Labels

MLObsML Observability (LLMObs)bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions