-
Notifications
You must be signed in to change notification settings - Fork 591
Fix Pydantic AI docs in integration class docstring (Fixes #5293) #5729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
bd0b816
85279b5
bcce29d
556b6d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,13 @@ | |
| from sentry_sdk.tracing_utils import _get_value, set_span_errored | ||
| from sentry_sdk.utils import capture_internal_exceptions, logger | ||
|
|
||
| CURRENT_LANGCHAIN_AGENT_NAME = contextvars.ContextVar("CURRENT_LANGCHAIN_AGENT_NAME", default=None) | ||
|
|
||
|
|
||
| def _get_current_langchain_agent_name() -> "Optional[str]": | ||
| return CURRENT_LANGCHAIN_AGENT_NAME.get(None) | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import ( | ||
| Any, | ||
|
|
@@ -154,43 +161,6 @@ def _transform_langchain_message_content(content: "Any") -> "Any": | |
|
|
||
|
|
||
| # Contextvar to track agent names in a stack for re-entrant agent support | ||
| _agent_stack: "contextvars.ContextVar[Optional[List[Optional[str]]]]" = ( | ||
| contextvars.ContextVar("langchain_agent_stack", default=None) | ||
| ) | ||
|
|
||
|
|
||
| def _push_agent(agent_name: "Optional[str]") -> None: | ||
| """Push an agent name onto the stack.""" | ||
| stack = _agent_stack.get() | ||
| if stack is None: | ||
| stack = [] | ||
| else: | ||
| # Copy the list to maintain contextvar isolation across async contexts | ||
| stack = stack.copy() | ||
| stack.append(agent_name) | ||
| _agent_stack.set(stack) | ||
|
|
||
|
|
||
| def _pop_agent() -> "Optional[str]": | ||
| """Pop an agent name from the stack and return it.""" | ||
| stack = _agent_stack.get() | ||
| if stack: | ||
| # Copy the list to maintain contextvar isolation across async contexts | ||
| stack = stack.copy() | ||
| agent_name = stack.pop() | ||
| _agent_stack.set(stack) | ||
| return agent_name | ||
| return None | ||
|
|
||
|
|
||
| def _get_current_agent() -> "Optional[str]": | ||
| """Get the current agent name (top of stack) without removing it.""" | ||
| stack = _agent_stack.get() | ||
| if stack: | ||
| return stack[-1] | ||
| return None | ||
|
|
||
|
|
||
| def _get_system_instructions(messages: "List[List[BaseMessage]]") -> "List[str]": | ||
| system_instructions = [] | ||
|
|
||
|
|
@@ -327,6 +297,11 @@ def _create_span( | |
| watched_span = WatchedSpan(sentry_sdk.start_span(**kwargs)) | ||
|
|
||
| watched_span.span.__enter__() | ||
|
|
||
| agent_name = _get_current_langchain_agent_name() | ||
| if agent_name: | ||
| watched_span.span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name) | ||
|
|
||
| self.span_map[run_id] = watched_span | ||
| self.gc_span_map() | ||
| return watched_span | ||
|
|
@@ -455,10 +430,6 @@ def on_chat_model_start( | |
| elif "openai" in ai_type: | ||
| span.set_data(SPANDATA.GEN_AI_SYSTEM, "openai") | ||
|
|
||
| agent_name = _get_current_agent() | ||
| if agent_name: | ||
| span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name) | ||
|
|
||
| for key, attribute in DATA_FIELDS.items(): | ||
| if key in all_params and all_params[key] is not None: | ||
| set_data_normalized(span, attribute, all_params[key], unpack=False) | ||
|
|
@@ -655,10 +626,6 @@ def on_tool_start( | |
| if tool_description is not None: | ||
| span.set_data(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool_description) | ||
|
|
||
| agent_name = _get_current_agent() | ||
| if agent_name: | ||
| span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name) | ||
|
|
||
| if should_send_default_pii() and self.include_prompts: | ||
| set_data_normalized( | ||
| span, | ||
|
|
@@ -978,17 +945,15 @@ def new_invoke(self: "Any", *args: "Any", **kwargs: "Any") -> "Any": | |
| return f(self, *args, **kwargs) | ||
|
|
||
| agent_name, tools = _get_request_data(self, args, kwargs) | ||
| token = CURRENT_LANGCHAIN_AGENT_NAME.set(agent_name) | ||
| start_span_function = get_start_span_function() | ||
|
|
||
| with start_span_function( | ||
| try: | ||
sentry[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| op=OP.GEN_AI_INVOKE_AGENT, | ||
| name=f"invoke_agent {agent_name}" if agent_name else "invoke_agent", | ||
| origin=LangchainIntegration.origin, | ||
| ) as span: | ||
sentry[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _push_agent(agent_name) | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try: | ||
| if agent_name: | ||
| span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name) | ||
|
|
||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "invoke_agent") | ||
| span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, False) | ||
|
|
@@ -1028,7 +993,8 @@ def new_invoke(self: "Any", *args: "Any", **kwargs: "Any") -> "Any": | |
| return result | ||
| finally: | ||
| # Ensure agent is popped even if an exception occurs | ||
| _pop_agent() | ||
| finally: | ||
| CURRENT_LANGCHAIN_AGENT_NAME.reset(token) | ||
|
|
||
| return new_invoke | ||
|
|
||
|
|
@@ -1041,6 +1007,7 @@ def new_stream(self: "Any", *args: "Any", **kwargs: "Any") -> "Any": | |
| return f(self, *args, **kwargs) | ||
|
|
||
| agent_name, tools = _get_request_data(self, args, kwargs) | ||
| token = CURRENT_LANGCHAIN_AGENT_NAME.set(agent_name) | ||
| start_span_function = get_start_span_function() | ||
|
|
||
| span = start_span_function( | ||
|
|
@@ -1050,37 +1017,37 @@ def new_stream(self: "Any", *args: "Any", **kwargs: "Any") -> "Any": | |
| ) | ||
| span.__enter__() | ||
|
|
||
| _push_agent(agent_name) | ||
|
|
||
| if agent_name: | ||
| span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name) | ||
|
|
||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "invoke_agent") | ||
| span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, True) | ||
| try: | ||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "invoke_agent") | ||
| span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, True) | ||
|
|
||
| _set_tools_on_span(span, tools) | ||
| _set_tools_on_span(span, tools) | ||
|
|
||
| input = args[0].get("input") if len(args) >= 1 else None | ||
| if ( | ||
| input is not None | ||
| and should_send_default_pii() | ||
| and integration.include_prompts | ||
| ): | ||
| normalized_messages = normalize_message_roles([input]) | ||
| scope = sentry_sdk.get_current_scope() | ||
| messages_data = truncate_and_annotate_messages( | ||
| normalized_messages, span, scope | ||
| ) | ||
| if messages_data is not None: | ||
| set_data_normalized( | ||
| span, | ||
| SPANDATA.GEN_AI_REQUEST_MESSAGES, | ||
| messages_data, | ||
| unpack=False, | ||
| input = args[0].get("input") if len(args) >= 1 else None | ||
| if ( | ||
| input is not None | ||
| and should_send_default_pii() | ||
| and integration.include_prompts | ||
| ): | ||
| normalized_messages = normalize_message_roles([input]) | ||
| scope = sentry_sdk.get_current_scope() | ||
| messages_data = truncate_and_annotate_messages( | ||
| normalized_messages, span, scope | ||
| ) | ||
| if messages_data is not None: | ||
| set_data_normalized( | ||
| span, | ||
| SPANDATA.GEN_AI_REQUEST_MESSAGES, | ||
| messages_data, | ||
| unpack=False, | ||
| ) | ||
|
|
||
| # Run the agent | ||
| result = f(self, *args, **kwargs) | ||
| # Run the agent | ||
| result = f(self, *args, **kwargs) | ||
| except Exception: | ||
| span.__exit__(None, None, None) | ||
| CURRENT_LANGCHAIN_AGENT_NAME.reset(token) | ||
| raise | ||
|
|
||
| old_iterator = result | ||
|
|
||
|
|
@@ -1107,8 +1074,8 @@ def new_iterator() -> "Iterator[Any]": | |
| raise | ||
| finally: | ||
| # Ensure cleanup happens even if iterator is abandoned or fails | ||
| _pop_agent() | ||
| span.__exit__(*exc_info) | ||
| CURRENT_LANGCHAIN_AGENT_NAME.reset(token) | ||
|
|
||
| async def new_iterator_async() -> "AsyncIterator[Any]": | ||
| exc_info: "tuple[Any, Any, Any]" = (None, None, None) | ||
|
|
@@ -1133,8 +1100,8 @@ async def new_iterator_async() -> "AsyncIterator[Any]": | |
| raise | ||
| finally: | ||
| # Ensure cleanup happens even if iterator is abandoned or fails | ||
| _pop_agent() | ||
| span.__exit__(*exc_info) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Calling Suggested FixThe Prompt for AI Agent |
||
| CURRENT_LANGCHAIN_AGENT_NAME.reset(token) | ||
|
|
||
| if str(type(result)) == "<class 'async_generator'>": | ||
| result = new_iterator_async() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stale comment now misleadingly annotates unrelated function
Low Severity
The comment
# Contextvar to track agent names in a stack for re-entrant agent supportoriginally documented the_agent_stackContextVar that was removed in this diff. The comment was left behind and now sits directly above_get_system_instructions, making it look like it describes that unrelated function. This is misleading for anyone reading the code.