Skip to content

Commit

Permalink
feat: Protect Langchain callbacks (#1653)
Browse files Browse the repository at this point in the history
* Add graceful fallback to langchain tracer

* Address feedback

* Rename fallback and add informative comments
  • Loading branch information
anticorrelator authored Oct 25, 2023
1 parent 67c1c26 commit 196c22b
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/phoenix/trace/langchain/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@
import logging
from copy import deepcopy
from datetime import datetime
from typing import (
Any,
Dict,
Iterable,
Iterator,
List,
Mapping,
Optional,
Tuple,
)
from typing import Any, Dict, Iterable, Iterator, List, Mapping, Optional, Tuple
from uuid import UUID

from langchain.callbacks.tracers.base import BaseTracer
Expand All @@ -20,13 +11,7 @@
from langchain.schema.messages import BaseMessage

from phoenix.trace.exporter import HttpExporter
from phoenix.trace.schemas import (
Span,
SpanEvent,
SpanException,
SpanKind,
SpanStatusCode,
)
from phoenix.trace.schemas import Span, SpanEvent, SpanException, SpanKind, SpanStatusCode
from phoenix.trace.semantic_conventions import (
DOCUMENT_CONTENT,
DOCUMENT_METADATA,
Expand Down Expand Up @@ -56,6 +41,7 @@
MimeType,
)
from phoenix.trace.tracer import Tracer
from phoenix.utilities.error_handling import graceful_fallback

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -279,6 +265,21 @@ def _retrieval_documents(
]


def _chat_model_start_fallback(
serialized: Dict[str, Any],
messages: List[List[BaseMessage]],
*,
run_id: UUID,
tags: Optional[List[str]] = None,
parent_run_id: Optional[UUID] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> None:
# Currently does nothing. If a functional fallback is implemented, new failures will not be
# caught
pass


class OpenInferenceTracer(Tracer, BaseTracer):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -351,6 +352,7 @@ def _persist_run(self, run: Run) -> None:
except Exception:
logger.exception("Failed to convert run to spans")

@graceful_fallback(_chat_model_start_fallback)
def on_chat_model_start(
self,
serialized: Dict[str, Any],
Expand Down

0 comments on commit 196c22b

Please sign in to comment.