Repository navigation
fix(smolagents): suspend stream invocations between reads #1006
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
Changes from all commits
e6bc1e8
0a6e9fa
4cbc7ca
9512eb2
2b17c6f
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Restore the caller's context while a model or agent run stream is unconsumed, so caller work is not parented under the `chat` or `invoke_agent` span. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,7 +44,7 @@ | |
| error_attributes, | ||
| server_attributes, | ||
| ) | ||
| from opentelemetry.trace import StatusCode | ||
| from opentelemetry.trace import INVALID_SPAN, StatusCode, get_current_span | ||
| from opentelemetry.util.genai.types import BlobPart, TextPart, UriPart | ||
|
|
||
| from .test_utils import ( | ||
|
|
@@ -652,6 +652,45 @@ def test_generate_stream_is_lazy_and_records_the_drained_response( | |
| assert attr(span, GenAI.GEN_AI_RESPONSE_FINISH_REASONS) is None | ||
|
|
||
|
|
||
| def test_generate_stream_is_current_only_while_read( | ||
| instrument_with_content, span_exporter, tracer_provider | ||
| ) -> None: | ||
| model = transformers_model(stream_chunks=["In ", "Paris"]) | ||
| stream = model.generate_stream(messages=MESSAGES) | ||
| assert get_current_span() is INVALID_SPAN | ||
|
|
||
| next(stream) | ||
| assert get_current_span() is INVALID_SPAN | ||
| with tracer_provider.get_tracer("test").start_as_current_span("caller"): | ||
| pass | ||
| list(stream) | ||
|
|
||
| (caller,) = [ | ||
| span | ||
| for span in span_exporter.get_finished_spans() | ||
| if span.name == "caller" | ||
| ] | ||
| assert caller.parent is None | ||
|
Member
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. This covers the caller side, but nothing checks that def test_generate_stream_reads_under_the_chat_span(
instrument_with_content, span_exporter
) -> None:
model = transformers_model()
seen = []
def chunks():
for text in ["In ", "Paris"]:
seen.append(get_current_span().get_span_context().span_id)
yield text
model.streamer = chunks()
list(model.generate_stream(messages=MESSAGES))
(chat,) = spans_by_operation(span_exporter.get_finished_spans(), "chat")
assert seen == [chat.context.span_id] * 2
Contributor
Author
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. added it in 2b17c6f, thanks! |
||
|
|
||
|
|
||
| def test_generate_stream_reads_under_the_chat_span( | ||
| instrument_with_content, span_exporter | ||
| ) -> None: | ||
| model = transformers_model() | ||
| seen = [] | ||
|
|
||
| def chunks(): | ||
| for text in ["In ", "Paris"]: | ||
| seen.append(get_current_span().get_span_context().span_id) | ||
| yield text | ||
|
|
||
| model.streamer = chunks() | ||
| list(model.generate_stream(messages=MESSAGES)) | ||
|
|
||
| (chat,) = spans_by_operation(span_exporter.get_finished_spans(), "chat") | ||
| assert seen == [chat.context.span_id] * 2 | ||
|
|
||
|
|
||
| def test_generate_stream_stays_a_generator( | ||
| instrument_with_content, span_exporter | ||
| ) -> None: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.