-
Notifications
You must be signed in to change notification settings - Fork 644
fix(client.spans): improve error messages for missing fields #9797
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: main
Are you sure you want to change the base?
fix(client.spans): improve error messages for missing fields #9797
Conversation
…existent projects
| {"name", "context", "span_kind", "start_time", "end_time", "status_code"} | ||
| ) | ||
| else: | ||
| required = Span.__required_keys__ |
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.
Bug: Span Type Missing Required Keys Attribute
The _extract_invalid_span_from_log_spans_error function attempts to use Span.__required_keys__ on Python 3.11+ to provide specific error messages for missing span fields. However, Span is an alias for a generated type (v1.Span) that may not expose this attribute, causing an AttributeError at runtime and preventing helpful error messages.
|
Hi @anticorrelator, whenever you get a chance, could you please take a look at this PR and let me know if any changes are needed? |
|
I had a couple of follow-up questions regarding this issue and the recent fix:
Would love to hear your thoughts on these |
|
|
||
| span: Any = spans[span_index] | ||
| span_dict = cast(dict[str, Any], span) | ||
|
|
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.
Instead of two strategies that depends on the Python version let's just use one
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.
Hi @anticorrelator,
I added the version-specific logic because SpanDataType inherits from TypedDict, and the NotRequired marker is ignored at runtime in Python ≤3.10, meaning all keys are treated as required in those versions. From 3.11 onwards, we can correctly extract required keys via required_keys.
That’s why I went with the version-dependent approach as a fallback. But I’m happy to explore a cleaner or more consistent strategy if you have any suggestions
I’d love to learn a better way to handle this.
anticorrelator
left a comment
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.
Thanks for the contribution! We don't need to warn on non-existent projects anymore and we should probably use a different strategy for letting users know about missing fields on Spans
|
One possible approach could be to define a constant (e.g. DEFAULT_REQUIRED_KEYS) and use that instead of the version check. The only trade-off is that we’d need to manually keep that list in sync if the required fields ever change. Would you prefer that I move ahead with this approach, or would you recommend another direction here? Just wanted to confirm before making the change. |
Resolves: #9500
This PR enhances the client.spans.log_spans error handling to provide clearer and more actionable feedback when span creation fails.
Use this notebook for quick replication: https://colab.research.google.com/drive/1cA7c3aVZrzx_-inVrNewebqqJFF1Hghi?usp=sharing
Note
Raise SpanCreationError with a descriptive message on 404 project lookups and emit explicit missing-key details in validation errors; add/update integration tests.
log_spans: on404now raisesSpanCreationErrorwith "Project not found" guidance instead of generic HTTP error._extract_invalid_span_from_log_spans_error):['status_code']), accounting for Python ≤3.10 vs ≥3.11 (Span.__required_keys__).systo checkversion_info.tests/integration/client/test_spans.py):SpanCreationErrorwith "Project not found".Written by Cursor Bugbot for commit e758d39. This will update automatically on new commits. Configure here.