mantisdk 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mantisdk might be problematic. Click here for more details.

Files changed (190) hide show
  1. mantisdk/__init__.py +22 -0
  2. mantisdk/adapter/__init__.py +15 -0
  3. mantisdk/adapter/base.py +94 -0
  4. mantisdk/adapter/messages.py +270 -0
  5. mantisdk/adapter/triplet.py +1028 -0
  6. mantisdk/algorithm/__init__.py +39 -0
  7. mantisdk/algorithm/apo/__init__.py +5 -0
  8. mantisdk/algorithm/apo/apo.py +889 -0
  9. mantisdk/algorithm/apo/prompts/apply_edit_variant01.poml +22 -0
  10. mantisdk/algorithm/apo/prompts/apply_edit_variant02.poml +18 -0
  11. mantisdk/algorithm/apo/prompts/text_gradient_variant01.poml +18 -0
  12. mantisdk/algorithm/apo/prompts/text_gradient_variant02.poml +16 -0
  13. mantisdk/algorithm/apo/prompts/text_gradient_variant03.poml +107 -0
  14. mantisdk/algorithm/base.py +162 -0
  15. mantisdk/algorithm/decorator.py +264 -0
  16. mantisdk/algorithm/fast.py +250 -0
  17. mantisdk/algorithm/gepa/__init__.py +59 -0
  18. mantisdk/algorithm/gepa/adapter.py +459 -0
  19. mantisdk/algorithm/gepa/gepa.py +364 -0
  20. mantisdk/algorithm/gepa/lib/__init__.py +18 -0
  21. mantisdk/algorithm/gepa/lib/adapters/README.md +12 -0
  22. mantisdk/algorithm/gepa/lib/adapters/__init__.py +0 -0
  23. mantisdk/algorithm/gepa/lib/adapters/anymaths_adapter/README.md +341 -0
  24. mantisdk/algorithm/gepa/lib/adapters/anymaths_adapter/__init__.py +1 -0
  25. mantisdk/algorithm/gepa/lib/adapters/anymaths_adapter/anymaths_adapter.py +174 -0
  26. mantisdk/algorithm/gepa/lib/adapters/anymaths_adapter/requirements.txt +1 -0
  27. mantisdk/algorithm/gepa/lib/adapters/default_adapter/README.md +0 -0
  28. mantisdk/algorithm/gepa/lib/adapters/default_adapter/__init__.py +0 -0
  29. mantisdk/algorithm/gepa/lib/adapters/default_adapter/default_adapter.py +209 -0
  30. mantisdk/algorithm/gepa/lib/adapters/dspy_adapter/README.md +7 -0
  31. mantisdk/algorithm/gepa/lib/adapters/dspy_adapter/__init__.py +0 -0
  32. mantisdk/algorithm/gepa/lib/adapters/dspy_adapter/dspy_adapter.py +307 -0
  33. mantisdk/algorithm/gepa/lib/adapters/dspy_full_program_adapter/README.md +99 -0
  34. mantisdk/algorithm/gepa/lib/adapters/dspy_full_program_adapter/dspy_program_proposal_signature.py +137 -0
  35. mantisdk/algorithm/gepa/lib/adapters/dspy_full_program_adapter/full_program_adapter.py +266 -0
  36. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/GEPA_RAG.md +621 -0
  37. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/__init__.py +56 -0
  38. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/evaluation_metrics.py +226 -0
  39. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/generic_rag_adapter.py +496 -0
  40. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/rag_pipeline.py +238 -0
  41. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_store_interface.py +212 -0
  42. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/__init__.py +2 -0
  43. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/chroma_store.py +196 -0
  44. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/lancedb_store.py +422 -0
  45. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/milvus_store.py +409 -0
  46. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/qdrant_store.py +368 -0
  47. mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/weaviate_store.py +418 -0
  48. mantisdk/algorithm/gepa/lib/adapters/mcp_adapter/README.md +552 -0
  49. mantisdk/algorithm/gepa/lib/adapters/mcp_adapter/__init__.py +37 -0
  50. mantisdk/algorithm/gepa/lib/adapters/mcp_adapter/mcp_adapter.py +705 -0
  51. mantisdk/algorithm/gepa/lib/adapters/mcp_adapter/mcp_client.py +364 -0
  52. mantisdk/algorithm/gepa/lib/adapters/terminal_bench_adapter/README.md +9 -0
  53. mantisdk/algorithm/gepa/lib/adapters/terminal_bench_adapter/__init__.py +0 -0
  54. mantisdk/algorithm/gepa/lib/adapters/terminal_bench_adapter/terminal_bench_adapter.py +217 -0
  55. mantisdk/algorithm/gepa/lib/api.py +375 -0
  56. mantisdk/algorithm/gepa/lib/core/__init__.py +0 -0
  57. mantisdk/algorithm/gepa/lib/core/adapter.py +180 -0
  58. mantisdk/algorithm/gepa/lib/core/data_loader.py +74 -0
  59. mantisdk/algorithm/gepa/lib/core/engine.py +356 -0
  60. mantisdk/algorithm/gepa/lib/core/result.py +233 -0
  61. mantisdk/algorithm/gepa/lib/core/state.py +636 -0
  62. mantisdk/algorithm/gepa/lib/examples/__init__.py +0 -0
  63. mantisdk/algorithm/gepa/lib/examples/aime.py +24 -0
  64. mantisdk/algorithm/gepa/lib/examples/anymaths-bench/eval_default.py +111 -0
  65. mantisdk/algorithm/gepa/lib/examples/anymaths-bench/prompt-templates/instruction_prompt.txt +9 -0
  66. mantisdk/algorithm/gepa/lib/examples/anymaths-bench/prompt-templates/optimal_prompt.txt +24 -0
  67. mantisdk/algorithm/gepa/lib/examples/anymaths-bench/train_anymaths.py +177 -0
  68. mantisdk/algorithm/gepa/lib/examples/dspy_full_program_evolution/arc_agi.ipynb +25705 -0
  69. mantisdk/algorithm/gepa/lib/examples/dspy_full_program_evolution/example.ipynb +348 -0
  70. mantisdk/algorithm/gepa/lib/examples/mcp_adapter/__init__.py +4 -0
  71. mantisdk/algorithm/gepa/lib/examples/mcp_adapter/mcp_optimization_example.py +455 -0
  72. mantisdk/algorithm/gepa/lib/examples/rag_adapter/RAG_GUIDE.md +613 -0
  73. mantisdk/algorithm/gepa/lib/examples/rag_adapter/__init__.py +9 -0
  74. mantisdk/algorithm/gepa/lib/examples/rag_adapter/rag_optimization.py +824 -0
  75. mantisdk/algorithm/gepa/lib/examples/rag_adapter/requirements-rag.txt +29 -0
  76. mantisdk/algorithm/gepa/lib/examples/terminal-bench/prompt-templates/instruction_prompt.txt +16 -0
  77. mantisdk/algorithm/gepa/lib/examples/terminal-bench/prompt-templates/terminus.txt +9 -0
  78. mantisdk/algorithm/gepa/lib/examples/terminal-bench/train_terminus.py +161 -0
  79. mantisdk/algorithm/gepa/lib/gepa_utils.py +117 -0
  80. mantisdk/algorithm/gepa/lib/logging/__init__.py +0 -0
  81. mantisdk/algorithm/gepa/lib/logging/experiment_tracker.py +187 -0
  82. mantisdk/algorithm/gepa/lib/logging/logger.py +75 -0
  83. mantisdk/algorithm/gepa/lib/logging/utils.py +103 -0
  84. mantisdk/algorithm/gepa/lib/proposer/__init__.py +0 -0
  85. mantisdk/algorithm/gepa/lib/proposer/base.py +31 -0
  86. mantisdk/algorithm/gepa/lib/proposer/merge.py +357 -0
  87. mantisdk/algorithm/gepa/lib/proposer/reflective_mutation/__init__.py +0 -0
  88. mantisdk/algorithm/gepa/lib/proposer/reflective_mutation/base.py +49 -0
  89. mantisdk/algorithm/gepa/lib/proposer/reflective_mutation/reflective_mutation.py +176 -0
  90. mantisdk/algorithm/gepa/lib/py.typed +0 -0
  91. mantisdk/algorithm/gepa/lib/strategies/__init__.py +0 -0
  92. mantisdk/algorithm/gepa/lib/strategies/batch_sampler.py +77 -0
  93. mantisdk/algorithm/gepa/lib/strategies/candidate_selector.py +50 -0
  94. mantisdk/algorithm/gepa/lib/strategies/component_selector.py +36 -0
  95. mantisdk/algorithm/gepa/lib/strategies/eval_policy.py +64 -0
  96. mantisdk/algorithm/gepa/lib/strategies/instruction_proposal.py +127 -0
  97. mantisdk/algorithm/gepa/lib/utils/__init__.py +10 -0
  98. mantisdk/algorithm/gepa/lib/utils/stop_condition.py +196 -0
  99. mantisdk/algorithm/gepa/tracing.py +105 -0
  100. mantisdk/algorithm/utils.py +177 -0
  101. mantisdk/algorithm/verl/__init__.py +5 -0
  102. mantisdk/algorithm/verl/interface.py +202 -0
  103. mantisdk/cli/__init__.py +56 -0
  104. mantisdk/cli/prometheus.py +115 -0
  105. mantisdk/cli/store.py +131 -0
  106. mantisdk/cli/vllm.py +29 -0
  107. mantisdk/client.py +408 -0
  108. mantisdk/config.py +348 -0
  109. mantisdk/emitter/__init__.py +43 -0
  110. mantisdk/emitter/annotation.py +370 -0
  111. mantisdk/emitter/exception.py +54 -0
  112. mantisdk/emitter/message.py +61 -0
  113. mantisdk/emitter/object.py +117 -0
  114. mantisdk/emitter/reward.py +320 -0
  115. mantisdk/env_var.py +156 -0
  116. mantisdk/execution/__init__.py +15 -0
  117. mantisdk/execution/base.py +64 -0
  118. mantisdk/execution/client_server.py +443 -0
  119. mantisdk/execution/events.py +69 -0
  120. mantisdk/execution/inter_process.py +16 -0
  121. mantisdk/execution/shared_memory.py +282 -0
  122. mantisdk/instrumentation/__init__.py +119 -0
  123. mantisdk/instrumentation/agentops.py +314 -0
  124. mantisdk/instrumentation/agentops_langchain.py +45 -0
  125. mantisdk/instrumentation/litellm.py +83 -0
  126. mantisdk/instrumentation/vllm.py +81 -0
  127. mantisdk/instrumentation/weave.py +500 -0
  128. mantisdk/litagent/__init__.py +11 -0
  129. mantisdk/litagent/decorator.py +536 -0
  130. mantisdk/litagent/litagent.py +252 -0
  131. mantisdk/llm_proxy.py +1890 -0
  132. mantisdk/logging.py +370 -0
  133. mantisdk/reward.py +7 -0
  134. mantisdk/runner/__init__.py +11 -0
  135. mantisdk/runner/agent.py +845 -0
  136. mantisdk/runner/base.py +182 -0
  137. mantisdk/runner/legacy.py +309 -0
  138. mantisdk/semconv.py +170 -0
  139. mantisdk/server.py +401 -0
  140. mantisdk/store/__init__.py +23 -0
  141. mantisdk/store/base.py +897 -0
  142. mantisdk/store/client_server.py +2092 -0
  143. mantisdk/store/collection/__init__.py +30 -0
  144. mantisdk/store/collection/base.py +587 -0
  145. mantisdk/store/collection/memory.py +970 -0
  146. mantisdk/store/collection/mongo.py +1412 -0
  147. mantisdk/store/collection_based.py +1823 -0
  148. mantisdk/store/insight.py +648 -0
  149. mantisdk/store/listener.py +58 -0
  150. mantisdk/store/memory.py +396 -0
  151. mantisdk/store/mongo.py +165 -0
  152. mantisdk/store/sqlite.py +3 -0
  153. mantisdk/store/threading.py +357 -0
  154. mantisdk/store/utils.py +142 -0
  155. mantisdk/tracer/__init__.py +16 -0
  156. mantisdk/tracer/agentops.py +242 -0
  157. mantisdk/tracer/base.py +287 -0
  158. mantisdk/tracer/dummy.py +106 -0
  159. mantisdk/tracer/otel.py +555 -0
  160. mantisdk/tracer/weave.py +677 -0
  161. mantisdk/trainer/__init__.py +6 -0
  162. mantisdk/trainer/init_utils.py +263 -0
  163. mantisdk/trainer/legacy.py +367 -0
  164. mantisdk/trainer/registry.py +12 -0
  165. mantisdk/trainer/trainer.py +618 -0
  166. mantisdk/types/__init__.py +6 -0
  167. mantisdk/types/core.py +553 -0
  168. mantisdk/types/resources.py +204 -0
  169. mantisdk/types/tracer.py +515 -0
  170. mantisdk/types/tracing.py +218 -0
  171. mantisdk/utils/__init__.py +1 -0
  172. mantisdk/utils/id.py +18 -0
  173. mantisdk/utils/metrics.py +1025 -0
  174. mantisdk/utils/otel.py +578 -0
  175. mantisdk/utils/otlp.py +536 -0
  176. mantisdk/utils/server_launcher.py +1045 -0
  177. mantisdk/utils/system_snapshot.py +81 -0
  178. mantisdk/verl/__init__.py +8 -0
  179. mantisdk/verl/__main__.py +6 -0
  180. mantisdk/verl/async_server.py +46 -0
  181. mantisdk/verl/config.yaml +27 -0
  182. mantisdk/verl/daemon.py +1154 -0
  183. mantisdk/verl/dataset.py +44 -0
  184. mantisdk/verl/entrypoint.py +248 -0
  185. mantisdk/verl/trainer.py +549 -0
  186. mantisdk-0.1.0.dist-info/METADATA +119 -0
  187. mantisdk-0.1.0.dist-info/RECORD +190 -0
  188. mantisdk-0.1.0.dist-info/WHEEL +4 -0
  189. mantisdk-0.1.0.dist-info/entry_points.txt +2 -0
  190. mantisdk-0.1.0.dist-info/licenses/LICENSE +19 -0
mantisdk/utils/otel.py ADDED
@@ -0,0 +1,578 @@
1
+ # Copyright (c) Microsoft. All rights reserved.
2
+
3
+ """Utilities shared for OpenTelemetry span (attributes) support."""
4
+
5
+ import json
6
+ import logging
7
+ import traceback
8
+ from typing import Any, Dict, List, Sequence, Type, TypeVar, Union, cast, Optional
9
+ from warnings import filterwarnings
10
+
11
+ import opentelemetry.trace as trace_api
12
+ from agentops.sdk.exporters import OTLPSpanExporter
13
+ from opentelemetry.sdk.trace import ReadableSpan, SpanLimits, SpanProcessor, SynchronousMultiSpanProcessor, Tracer
14
+ from opentelemetry.sdk.trace import TracerProvider as TracerProviderImpl
15
+ from opentelemetry.sdk.trace.export import BatchSpanProcessor, SimpleSpanProcessor
16
+ from opentelemetry.sdk.util.instrumentation import InstrumentationInfo, InstrumentationScope
17
+ from opentelemetry.semconv.attributes import exception_attributes
18
+ from opentelemetry.trace import get_tracer_provider as otel_get_tracer_provider
19
+ from pydantic import TypeAdapter
20
+
21
+ from mantisdk.env_var import LightningEnvVar, resolve_bool_env_var
22
+ from mantisdk.semconv import LightningSpanAttributes, LinkAttributes, LinkPydanticModel
23
+ from mantisdk.types import Attributes, AttributeValue, SpanLike
24
+ from mantisdk.types.tracing import get_current_call_type
25
+ from mantisdk.utils.otlp import LightningStoreOTLPExporter
26
+
27
+ logger = logging.getLogger(__name__)
28
+
29
+ __all__ = [
30
+ "full_qualified_name",
31
+ "get_tracer_provider",
32
+ "get_tracer",
33
+ "make_tag_attributes",
34
+ "extract_tags_from_attributes",
35
+ "make_link_attributes",
36
+ "query_linked_spans",
37
+ "extract_links_from_attributes",
38
+ "filter_attributes",
39
+ "filter_and_unflatten_attributes",
40
+ "flatten_attributes",
41
+ "unflatten_attributes",
42
+ "sanitize_attribute_value",
43
+ "sanitize_attributes",
44
+ "sanitize_list_attribute_sanity",
45
+ "check_attributes_sanity",
46
+ "format_exception_attributes",
47
+ ]
48
+
49
+ T_SpanLike = TypeVar("T_SpanLike", bound=SpanLike)
50
+ T_SpanProcessor = TypeVar("T_SpanProcessor", bound=SpanProcessor)
51
+
52
+
53
+ class CallTypeSpanProcessor(SpanProcessor):
54
+ """Span processor that adds mantis.call_type attribute from context.
55
+
56
+ This processor runs on start of every span and checks if a call_type
57
+ is set in the context (via decorators like @gepa.judge). If so, it
58
+ adds the `mantis.call_type` attribute to the span.
59
+ """
60
+
61
+ def on_start(self, span: Any, parent_context: Optional[Any] = None) -> None:
62
+ call_type = get_current_call_type()
63
+ if call_type:
64
+ span.set_attribute("mantis.call_type", call_type)
65
+
66
+ def on_end(self, span: Any) -> None:
67
+ pass
68
+
69
+ def shutdown(self) -> None:
70
+ pass
71
+
72
+ def force_flush(self, timeout_millis: int = 30000) -> bool:
73
+ return True
74
+
75
+
76
+ def full_qualified_name(obj: type) -> str:
77
+ if str(obj.__module__) == "builtins":
78
+ return obj.__qualname__
79
+ return f"{obj.__module__}.{obj.__qualname__}"
80
+
81
+
82
+ def get_tracer_provider(inspect: bool = True) -> TracerProviderImpl:
83
+ """Get the OpenTelemetry tracer provider configured for Mantisdk.
84
+
85
+ Args:
86
+ inspect: Whether to inspect the tracer provider and log its configuration.
87
+ When it's on, make sure you also set the logger level to DEBUG to see the logs.
88
+ """
89
+ from mantisdk.tracer.otel import LightningSpanProcessor
90
+
91
+ if hasattr(trace_api, "_TRACER_PROVIDER") and trace_api._TRACER_PROVIDER is None: # type: ignore[attr-defined]
92
+ raise RuntimeError("Tracer is not initialized. Cannot emit a meaningful span.")
93
+ tracer_provider = otel_get_tracer_provider()
94
+ if not isinstance(tracer_provider, TracerProviderImpl):
95
+ logger.error(
96
+ "Tracer provider is expected to be an instance of opentelemetry.sdk.trace.TracerProvider, found: %s",
97
+ full_qualified_name(type(tracer_provider)),
98
+ )
99
+ return cast(TracerProviderImpl, tracer_provider)
100
+
101
+ # Add CallTypeSpanProcessor if not already present
102
+ # This ensures spans get tagged with call_type from context
103
+ has_call_type_processor = False
104
+ for processor in tracer_provider._active_span_processor._span_processors: # pyright: ignore[reportPrivateUsage]
105
+ if isinstance(processor, CallTypeSpanProcessor):
106
+ has_call_type_processor = True
107
+ break
108
+
109
+ if not has_call_type_processor:
110
+ tracer_provider.add_span_processor(CallTypeSpanProcessor())
111
+
112
+ if not inspect:
113
+ return tracer_provider
114
+
115
+ emitter_debug = resolve_bool_env_var(LightningEnvVar.AGL_EMITTER_DEBUG, fallback=None)
116
+ logger_effective_level = logger.getEffectiveLevel()
117
+ if emitter_debug is True and logger_effective_level > logging.DEBUG:
118
+ logger.warning(
119
+ "Emitter debug logging is enabled but logging level is not set to DEBUG. Nothing will be logged."
120
+ )
121
+
122
+ if emitter_debug is None:
123
+ # Set to true by default if the logging level is lower than DEBUG
124
+ emitter_debug = logging.DEBUG >= logger_effective_level
125
+
126
+ if emitter_debug:
127
+ active_span_processor = tracer_provider._active_span_processor # pyright: ignore[reportPrivateUsage]
128
+ processors: List[str] = []
129
+ active_span_processor_cls = active_span_processor.__class__.__name__
130
+ for processor in active_span_processor._span_processors: # pyright: ignore[reportPrivateUsage]
131
+ if isinstance(processor, LightningSpanProcessor):
132
+ # The legacy case for tracers without OTLP support.
133
+ processors.append(f"{active_span_processor_cls} - {processor!r}")
134
+ elif isinstance(processor, (SimpleSpanProcessor, BatchSpanProcessor)):
135
+ processor_cls = processor.__class__.__name__
136
+ if isinstance(processor.span_exporter, LightningStoreOTLPExporter):
137
+ # This should be the main path now.
138
+ processors.append(f"{active_span_processor_cls} - {processor_cls} - {processor.span_exporter!r}")
139
+ elif isinstance(processor.span_exporter, OTLPSpanExporter):
140
+ # You need to be careful if the code goes into this path.
141
+ endpoint = processor.span_exporter._endpoint # pyright: ignore[reportPrivateUsage]
142
+ processors.append(
143
+ f"{active_span_processor_cls} - {processor_cls} - "
144
+ f"{processor.span_exporter.__class__.__name__}(endpoint={endpoint!r})"
145
+ )
146
+ else:
147
+ # Other cases like Console Span Exporter.
148
+ processors.append(
149
+ f"{active_span_processor_cls} - {processor_cls} - {processor.span_exporter.__class__.__name__}"
150
+ )
151
+ elif isinstance(processor, CallTypeSpanProcessor):
152
+ processors.append(f"{active_span_processor_cls} - {processor.__class__.__name__}")
153
+ else:
154
+ processors.append(f"{active_span_processor_cls} - {processor.__class__.__name__}")
155
+
156
+ logger.debug(f"Tracer provider: {tracer_provider!r}. Active span processors:")
157
+ for processor in processors:
158
+ logger.debug(" * " + processor)
159
+
160
+ return tracer_provider
161
+
162
+
163
+ def get_span_processors(
164
+ tracer_provider: TracerProviderImpl, expected_type: Type[T_SpanProcessor]
165
+ ) -> List[T_SpanProcessor]:
166
+ """Get the span processors from the tracer provider.
167
+
168
+ Args:
169
+ tracer_provider: The tracer provider to get the span processors from.
170
+ expected_type: The type of the span processors to get.
171
+
172
+ Returns:
173
+ A list of span processors of the expected type.
174
+ """
175
+ processors: List[T_SpanProcessor] = []
176
+ for processor in tracer_provider._active_span_processor._span_processors: # pyright: ignore[reportPrivateUsage]
177
+ if isinstance(processor, expected_type):
178
+ processors.append(processor)
179
+ return processors
180
+
181
+
182
+ def get_tracer(use_active_span_processor: bool = True) -> trace_api.Tracer:
183
+ """Resolve the OpenTelemetry tracer configured for Mantisdk.
184
+
185
+ Args:
186
+ use_active_span_processor: Whether to use the active span processor.
187
+
188
+ Returns:
189
+ OpenTelemetry tracer tagged with the `mantisdk` instrumentation name.
190
+
191
+ Raises:
192
+ RuntimeError: If OpenTelemetry was not initialized before calling this helper.
193
+ """
194
+ if hasattr(trace_api, "_TRACER_PROVIDER") and trace_api._TRACER_PROVIDER is None: # type: ignore[attr-defined]
195
+ raise RuntimeError("Tracer is not initialized. Cannot emit a meaningful span.")
196
+
197
+ tracer_provider = get_tracer_provider(inspect=True) # inspection is on by default
198
+
199
+ if use_active_span_processor:
200
+ return tracer_provider.get_tracer("mantisdk")
201
+
202
+ else:
203
+ filterwarnings(
204
+ "ignore",
205
+ message=r"You should use InstrumentationScope. Deprecated since version 1.11.1.",
206
+ category=DeprecationWarning,
207
+ module="opentelemetry.sdk.trace",
208
+ )
209
+
210
+ return Tracer(
211
+ tracer_provider.sampler,
212
+ tracer_provider.resource,
213
+ # We use an empty span processor to avoid emitting spans to the tracer
214
+ SynchronousMultiSpanProcessor(),
215
+ tracer_provider.id_generator,
216
+ InstrumentationInfo("mantisdk", "", ""), # type: ignore
217
+ SpanLimits(),
218
+ InstrumentationScope(
219
+ "mantisdk",
220
+ "",
221
+ "",
222
+ {},
223
+ ),
224
+ )
225
+
226
+
227
+ def make_tag_attributes(tags: List[str]) -> Dict[str, Any]:
228
+ """Convert a list of tags into flattened attributes for span tagging.
229
+
230
+ There is no syntax enforced for tags, they are just strings. For example:
231
+
232
+ ```python
233
+ ["gen_ai.model:gpt-4", "reward.extrinsic"]
234
+ ```
235
+ """
236
+ return flatten_attributes({LightningSpanAttributes.TAG.value: tags}, expand_leaf_lists=True)
237
+
238
+
239
+ def extract_tags_from_attributes(attributes: Dict[str, Any]) -> List[str]:
240
+ """Extract tag attributes from flattened span attributes.
241
+
242
+ Args:
243
+ attributes: A dictionary of flattened span attributes.
244
+ """
245
+ maybe_tag_list = filter_and_unflatten_attributes(attributes, LightningSpanAttributes.TAG.value)
246
+ return TypeAdapter(List[str]).validate_python(maybe_tag_list)
247
+
248
+
249
+ def make_link_attributes(links: Dict[str, str]) -> Dict[str, Any]:
250
+ """Convert a dictionary of links into flattened attributes for span linking.
251
+
252
+ Links example:
253
+
254
+ ```python
255
+ {
256
+ "gen_ai.response.id": "response-123",
257
+ "span_id": "abcd-efgh-ijkl",
258
+ }
259
+ ```
260
+ """
261
+ link_list: List[Dict[str, str]] = []
262
+ for key, value in links.items():
263
+ if not isinstance(value, str): # pyright: ignore[reportUnnecessaryIsInstance]
264
+ raise ValueError(f"Link value must be a string, got {type(value)} for key '{key}'")
265
+ link_list.append({LinkAttributes.KEY_MATCH.value: key, LinkAttributes.VALUE_MATCH.value: value})
266
+ return flatten_attributes({LightningSpanAttributes.LINK.value: link_list}, expand_leaf_lists=True)
267
+
268
+
269
+ def query_linked_spans(spans: Sequence[T_SpanLike], links: List[LinkPydanticModel]) -> List[T_SpanLike]:
270
+ """Query spans that are linked by the given link attributes.
271
+
272
+ Args:
273
+ spans: A sequence of spans to search.
274
+ links: A list of link attributes to match.
275
+
276
+ Returns:
277
+ A list of spans that match the given link attributes.
278
+ """
279
+ matched_spans: List[T_SpanLike] = []
280
+
281
+ for span in spans:
282
+ span_attributes = span.attributes or {}
283
+ is_match = True
284
+ for link in links:
285
+ # trace_id and span_id must be full match.
286
+ if link.key_match == "trace_id":
287
+ if isinstance(span, ReadableSpan):
288
+ trace_id = trace_api.format_trace_id(span.context.trace_id) if span.context else None
289
+ else:
290
+ trace_id = span.trace_id
291
+ if trace_id != link.value_match:
292
+ is_match = False
293
+ break
294
+
295
+ elif link.key_match == "span_id":
296
+ if isinstance(span, ReadableSpan):
297
+ span_id = trace_api.format_span_id(span.context.span_id) if span.context else None
298
+ else:
299
+ span_id = span.span_id
300
+ if span_id != link.value_match:
301
+ is_match = False
302
+ break
303
+
304
+ else:
305
+ attribute = span_attributes.get(link.key_match)
306
+ # attributes must also be a full match currently.
307
+ if attribute != link.value_match:
308
+ is_match = False
309
+ break
310
+
311
+ if is_match:
312
+ matched_spans.append(span)
313
+
314
+ return matched_spans
315
+
316
+
317
+ def extract_links_from_attributes(attributes: Dict[str, Any]) -> List[LinkPydanticModel]:
318
+ """Extract link attributes from flattened span attributes.
319
+
320
+ Args:
321
+ attributes: A dictionary of flattened span attributes.
322
+ """
323
+ maybe_link_list = filter_and_unflatten_attributes(attributes, LightningSpanAttributes.LINK.value)
324
+ return TypeAdapter(List[LinkPydanticModel]).validate_python(maybe_link_list)
325
+
326
+
327
+ def filter_attributes(attributes: Dict[str, Any], prefix: str) -> Dict[str, Any]:
328
+ """Filter attributes that start with the given prefix.
329
+
330
+ The attribute must start with `prefix.` or be exactly `prefix` to be included.
331
+
332
+ Args:
333
+ attributes: A dictionary of span attributes.
334
+ prefix: The prefix to filter by.
335
+
336
+ Returns:
337
+ A dictionary of attributes that start with the given prefix.
338
+ """
339
+ return {k: v for k, v in attributes.items() if k.startswith(prefix + ".") or k == prefix}
340
+
341
+
342
+ def filter_and_unflatten_attributes(attributes: Dict[str, Any], prefix: str) -> Union[Dict[str, Any], List[Any]]:
343
+ """Filter attributes that start with the given prefix and unflatten them.
344
+ The prefix will be removed during unflattening.
345
+
346
+ Args:
347
+ attributes: A dictionary of span attributes.
348
+ prefix: The prefix to filter by.
349
+
350
+ Returns:
351
+ A nested dictionary or list of attributes that start with the given prefix.
352
+ """
353
+ filtered_attributes = filter_attributes(attributes, prefix)
354
+ stripped_attributes: Dict[str, Any] = {}
355
+ for k, v in filtered_attributes.items():
356
+ if k == prefix:
357
+ raise ValueError(f"Cannot unflatten attribute with key exactly equal to prefix: {prefix}")
358
+ else:
359
+ stripped_key = k[len(prefix) + 1 :] # +1 to remove the dot
360
+ stripped_attributes[stripped_key] = v
361
+ return unflatten_attributes(stripped_attributes)
362
+
363
+
364
+ def flatten_attributes(
365
+ nested_data: Union[Dict[str, Any], List[Any]], *, expand_leaf_lists: bool = False
366
+ ) -> Dict[str, Any]:
367
+ """Flatten a nested dictionary or list into a flat dictionary with dotted keys.
368
+
369
+ This function recursively traverses dictionaries and lists, producing a flat
370
+ key-value mapping where nested paths are represented via dot-separated keys.
371
+ Lists are indexed numerically.
372
+
373
+ Example:
374
+
375
+ >>> flatten_attributes({"a": {"b": 1, "c": [2, 3]}}, expand_leaf_lists=True)
376
+ {"a.b": 1, "a.c.0": 2, "a.c.1": 3}
377
+
378
+ Args:
379
+ nested_data: A nested structure composed of dictionaries, lists, or primitive values.
380
+ expand_leaf_lists: Whether to expand lists composed only of primitive values.
381
+ When `False` (the default), lists of str/int/float/bool are treated as
382
+ leaf values and stored without enumerating their indices.
383
+
384
+ Returns:
385
+ A flat dictionary mapping dotted-string paths to primitive values.
386
+ """
387
+
388
+ flat: Dict[str, Any] = {}
389
+
390
+ def _primitive_type(value: Any) -> Union[type[str], type[int], type[float], type[bool]]:
391
+ if isinstance(value, bool):
392
+ return bool
393
+ if isinstance(value, int):
394
+ return int
395
+ if isinstance(value, float):
396
+ return float
397
+ return str
398
+
399
+ def _walk(value: Any, prefix: str = "") -> None:
400
+ if isinstance(value, dict):
401
+ for k, v in cast(Dict[Any, Any], value).items():
402
+ if not isinstance(k, str):
403
+ raise ValueError(
404
+ f"Only string keys are supported in dictionaries, got '{k}' of type {type(k)} in {prefix}"
405
+ )
406
+ new_prefix = f"{prefix}.{k}" if prefix else k
407
+ _walk(v, new_prefix)
408
+ elif isinstance(value, list):
409
+ maybe_list = cast(List[Any], value)
410
+ is_leaf_candidate = bool(maybe_list) and all(
411
+ isinstance(item, (str, int, float, bool)) for item in maybe_list
412
+ )
413
+ if not expand_leaf_lists and is_leaf_candidate and prefix:
414
+ primitive_types = {_primitive_type(item) for item in maybe_list}
415
+ if len(primitive_types) == 1:
416
+ flat[prefix] = maybe_list
417
+ return
418
+ logger.warning(
419
+ "List attribute '%s' contains mixed primitive types %s; expanding indexed keys instead.",
420
+ prefix,
421
+ primitive_types,
422
+ )
423
+
424
+ for idx, item in enumerate(maybe_list):
425
+ new_prefix = f"{prefix}.{idx}" if prefix else str(idx)
426
+ _walk(item, new_prefix)
427
+ else:
428
+ flat[prefix] = value
429
+
430
+ _walk(nested_data)
431
+ return flat
432
+
433
+
434
+ def unflatten_attributes(flat_data: Dict[str, Any]) -> Union[Dict[str, Any], List[Any]]:
435
+ """Reconstruct a nested dictionary/list structure from a flat dictionary.
436
+
437
+ Keys are dot-separated paths. Segments that are digit strings will only
438
+ become list indices if *all* keys in that dict form a consecutive
439
+ 0..n-1 range. Otherwise they remain dict keys.
440
+
441
+ Example:
442
+
443
+ >>> unflatten_attributes({"a.b": 1, "a.c.0": 2, "a.c.1": 3})
444
+ {"a": {"b": 1, "c": [2, 3]}}
445
+
446
+ Args:
447
+ flat_data: A dictionary whose keys are dot-separated paths and whose
448
+ values are primitive data elements.
449
+
450
+ Returns:
451
+ A nested dictionary (and lists where appropriate) corresponding to
452
+ the flattened structure.
453
+ """
454
+ # 1) Build a pure dict tree first (no lists yet)
455
+ root: Dict[str, Any] = {}
456
+
457
+ for flat_key, value in flat_data.items():
458
+ parts = flat_key.split(".")
459
+ curr: Dict[str, Any] = root
460
+
461
+ for part in parts[:-1]:
462
+ # Ensure intermediate node is a dict
463
+ if part not in curr or not isinstance(curr[part], dict):
464
+ curr[part] = {}
465
+ curr = curr[part] # type: ignore[assignment]
466
+
467
+ curr[parts[-1]] = value
468
+
469
+ # 2) Recursively convert dicts-with-consecutive-numeric-keys into lists
470
+ def convert(node: Union[Dict[str, Any], List[Any]]) -> Union[Dict[str, Any], List[Any]]:
471
+ if isinstance(node, dict):
472
+ # First convert children
473
+ for k, v in list(node.items()):
474
+ node[k] = convert(v)
475
+
476
+ if not node:
477
+ # empty dict stays dict
478
+ return node
479
+
480
+ # Check if keys are all numeric strings
481
+ keys = list(node.keys())
482
+ if all(isinstance(k, str) and k.isdigit() for k in keys): # pyright: ignore[reportUnnecessaryIsInstance]
483
+ indices = sorted(int(k) for k in keys)
484
+ # Must be exactly 0..n-1
485
+ if indices == list(range(len(indices))):
486
+ return [node[str(i)] for i in range(len(indices))]
487
+
488
+ return node
489
+
490
+ if isinstance(node, list): # pyright: ignore[reportUnnecessaryIsInstance]
491
+ return [convert(v) for v in node]
492
+
493
+ # Keep as is
494
+ return node
495
+
496
+ return convert(root)
497
+
498
+
499
+ def sanitize_attribute_value(object: Any, force: bool = True) -> AttributeValue:
500
+ """Sanitize an attribute value to be a valid OpenTelemetry attribute value."""
501
+ if isinstance(object, (str, int, float, bool)):
502
+ return object
503
+
504
+ if isinstance(object, list):
505
+ try:
506
+ return sanitize_list_attribute_sanity(cast(List[Any], object))
507
+ except ValueError as exc:
508
+ logger.warning(f"Failed to sanitize list attribute. Fallback to JSON serialization: {exc}")
509
+
510
+ try:
511
+ # This include null, dict, etc.
512
+ serialized = json.dumps(object, default=str if force else None)
513
+ except (TypeError, ValueError) as exc:
514
+ raise ValueError(f"Object must be JSON serializable, got: {type(cast(Any, object))}.") from exc
515
+ return serialized
516
+
517
+
518
+ def sanitize_attributes(attributes: Dict[str, Any], force: bool = True) -> Attributes:
519
+ """Sanitize a dictionary of attributes to be a valid OpenTelemetry attributes.
520
+
521
+ Args:
522
+ attributes: A dictionary of attributes to sanitize.
523
+ force: Whether to force sanitization even when the value is not JSON serializable.
524
+ """
525
+ result: Attributes = {}
526
+ for k, v in attributes.items():
527
+ try:
528
+ result[k] = sanitize_attribute_value(v, force=force)
529
+ except ValueError as exc:
530
+ raise ValueError(f"Failed to sanitize attribute '{k}': {exc}") from exc
531
+ return result
532
+
533
+
534
+ def sanitize_list_attribute_sanity(maybe_list: List[Any]) -> AttributeValue:
535
+ """Try to sanitize a list of attributes to be a valid OpenTelemetry attribute value.
536
+
537
+ Raise error if the list contains multiple types of primitive values.
538
+ """
539
+ if all(isinstance(item, str) for item in maybe_list):
540
+ return list[str](maybe_list)
541
+ if all(isinstance(item, bool) for item in maybe_list):
542
+ return list[bool](maybe_list)
543
+ if all(isinstance(item, (int, bool)) for item in maybe_list):
544
+ return [int(item) for item in maybe_list]
545
+ if all(isinstance(item, (float, int, bool)) for item in maybe_list):
546
+ return [float(item) for item in maybe_list]
547
+
548
+ list_types: List[Any] = [type(item) for item in maybe_list]
549
+ raise ValueError(f"List must contain only one type of primitive values, got: {set(list_types)}.")
550
+
551
+
552
+ def check_attributes_sanity(attributes: Dict[Any, Any]) -> None:
553
+ """Check if a dictionary of attributes is a valid OpenTelemetry attributes."""
554
+ for k, v in attributes.items():
555
+ if not isinstance(k, str):
556
+ raise ValueError(f"Attribute key must be a string, got {type(k)} for key '{k}'")
557
+ if isinstance(v, list):
558
+ try:
559
+ sanitize_list_attribute_sanity(cast(List[Any], v))
560
+ except ValueError as exc:
561
+ raise ValueError(f"Failed to sanitize list attribute '{k}': {exc}") from exc
562
+ elif not isinstance(v, (str, int, float, bool)):
563
+ raise ValueError(
564
+ f"Attribute value must be a string, int, float, bool, or list of these, got {type(v)} for value '{v}'"
565
+ )
566
+
567
+
568
+ def format_exception_attributes(exception: BaseException) -> Attributes:
569
+ """Format an exception into a dictionary of attributes."""
570
+ stacktrace = "".join(traceback.format_exception(type(exception), exception, exception.__traceback__))
571
+ span_attributes: Attributes = {
572
+ exception_attributes.EXCEPTION_TYPE: type(exception).__name__,
573
+ exception_attributes.EXCEPTION_MESSAGE: str(exception),
574
+ exception_attributes.EXCEPTION_ESCAPED: True,
575
+ }
576
+ if stacktrace.strip():
577
+ span_attributes[exception_attributes.EXCEPTION_STACKTRACE] = stacktrace
578
+ return span_attributes