@langfuse/tracing 4.0.0-beta.8 → 4.0.0-beta.9

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.
package/dist/index.d.cts CHANGED
@@ -1598,15 +1598,164 @@ declare function startActiveObservation<F extends (span: LangfuseSpan) => unknow
1598
1598
  * @public
1599
1599
  */
1600
1600
  declare function updateActiveTrace(attributes: LangfuseTraceAttributes): void;
1601
- declare function updateActiveObservation(currentType: "span", attributes: LangfuseSpanAttributes): void;
1602
- declare function updateActiveObservation(currentType: "generation", attributes: LangfuseGenerationAttributes): void;
1603
- declare function updateActiveObservation(currentType: "agent", attributes: LangfuseAgentAttributes): void;
1604
- declare function updateActiveObservation(currentType: "tool", attributes: LangfuseToolAttributes): void;
1605
- declare function updateActiveObservation(currentType: "chain", attributes: LangfuseChainAttributes): void;
1606
- declare function updateActiveObservation(currentType: "embedding", attributes: LangfuseEmbeddingAttributes): void;
1607
- declare function updateActiveObservation(currentType: "evaluator", attributes: LangfuseEvaluatorAttributes): void;
1608
- declare function updateActiveObservation(currentType: "guardrail", attributes: LangfuseGuardrailAttributes): void;
1609
- declare function updateActiveObservation(currentType: "retriever", attributes: LangfuseRetrieverAttributes): void;
1601
+ /**
1602
+ * Updates the currently active observation with new attributes.
1603
+ *
1604
+ * This function finds the currently active OpenTelemetry span in the execution context
1605
+ * and updates it with Langfuse-specific attributes. It supports all observation types
1606
+ * through TypeScript overloads, providing type safety for attributes based on the
1607
+ * specified `asType` parameter. If no active span exists, the update is skipped with a warning.
1608
+ *
1609
+ * ## Type Safety
1610
+ * - Automatic type inference based on `asType` parameter
1611
+ * - Compile-time validation of attribute compatibility
1612
+ * - IntelliSense support for observation-specific attributes
1613
+ *
1614
+ * ## Context Requirements
1615
+ * - Must be called within an active OpenTelemetry span context
1616
+ * - Typically used inside `startActiveObservation` callbacks or manual span contexts
1617
+ * - Relies on OpenTelemetry's context propagation mechanism
1618
+ *
1619
+ * ## Supported Observation Types
1620
+ * - **span** (default): General-purpose operations and workflows
1621
+ * - **generation**: LLM calls and AI model interactions
1622
+ * - **agent**: AI agent workflows with tool usage
1623
+ * - **tool**: Individual tool calls and API requests
1624
+ * - **chain**: Multi-step processes and pipelines
1625
+ * - **retriever**: Document retrieval and search operations
1626
+ * - **evaluator**: Quality assessment and scoring
1627
+ * - **guardrail**: Safety checks and content filtering
1628
+ * - **embedding**: Text embedding and vector operations
1629
+ *
1630
+ * @param attributes - Observation-specific attributes to update (type varies by observation type)
1631
+ * @param options - Configuration specifying observation type (defaults to 'span')
1632
+ *
1633
+ * @example
1634
+ * ```typescript
1635
+ * import { updateActiveObservation, startActiveObservation } from '@langfuse/tracing';
1636
+ *
1637
+ * // Update active span (default)
1638
+ * await startActiveObservation('data-processing', async (observation) => {
1639
+ * // Process data...
1640
+ * const result = await processData(inputData);
1641
+ *
1642
+ * // Update with results
1643
+ * updateActiveObservation({
1644
+ * output: { processedRecords: result.count },
1645
+ * metadata: { processingTime: result.duration }
1646
+ * });
1647
+ * });
1648
+ *
1649
+ * // Update active generation
1650
+ * await startActiveObservation('llm-call', async () => {
1651
+ * const response = await openai.chat.completions.create({
1652
+ * model: 'gpt-4',
1653
+ * messages: [{ role: 'user', content: 'Hello' }]
1654
+ * });
1655
+ *
1656
+ * // Update with LLM-specific attributes
1657
+ * updateActiveObservation({
1658
+ * output: response.choices[0].message,
1659
+ * usageDetails: {
1660
+ * promptTokens: response.usage.prompt_tokens,
1661
+ * completionTokens: response.usage.completion_tokens,
1662
+ * totalTokens: response.usage.total_tokens
1663
+ * },
1664
+ * costDetails: {
1665
+ * totalCost: 0.025,
1666
+ * currency: 'USD'
1667
+ * }
1668
+ * }, { asType: 'generation' });
1669
+ * }, {}, { asType: 'generation' });
1670
+ *
1671
+ * // Update active tool execution
1672
+ * await startActiveObservation('web-search', async () => {
1673
+ * const results = await searchAPI('latest news');
1674
+ *
1675
+ * updateActiveObservation({
1676
+ * output: {
1677
+ * results: results,
1678
+ * count: results.length,
1679
+ * relevanceScore: 0.89
1680
+ * },
1681
+ * metadata: {
1682
+ * searchLatency: 150,
1683
+ * cacheHit: false
1684
+ * }
1685
+ * }, { asType: 'tool' });
1686
+ * }, {}, { asType: 'tool' });
1687
+ *
1688
+ * // Update active agent workflow
1689
+ * await startActiveObservation('research-agent', async () => {
1690
+ * // Agent performs multiple operations...
1691
+ * const findings = await conductResearch();
1692
+ *
1693
+ * updateActiveObservation({
1694
+ * output: {
1695
+ * completed: true,
1696
+ * toolsUsed: ['web-search', 'summarizer'],
1697
+ * iterationsRequired: 3,
1698
+ * confidence: 0.92
1699
+ * },
1700
+ * metadata: {
1701
+ * efficiency: 0.85,
1702
+ * qualityScore: 0.88
1703
+ * }
1704
+ * }, { asType: 'agent' });
1705
+ * }, {}, { asType: 'agent' });
1706
+ *
1707
+ * // Update active chain workflow
1708
+ * await startActiveObservation('rag-pipeline', async () => {
1709
+ * // Execute multi-step RAG process...
1710
+ * const finalResponse = await executeRAGPipeline();
1711
+ *
1712
+ * updateActiveObservation({
1713
+ * output: {
1714
+ * finalResponse: finalResponse,
1715
+ * stepsCompleted: 4,
1716
+ * documentsRetrieved: 8,
1717
+ * qualityScore: 0.91
1718
+ * },
1719
+ * metadata: {
1720
+ * pipelineEfficiency: 0.87,
1721
+ * totalLatency: 3200
1722
+ * }
1723
+ * }, { asType: 'chain' });
1724
+ * }, {}, { asType: 'chain' });
1725
+ * ```
1726
+ *
1727
+ * @see {@link startActiveObservation} - For creating active observation contexts
1728
+ * @see {@link updateActiveTrace} - For updating trace-level attributes
1729
+ *
1730
+ * @public
1731
+ */
1732
+ declare function updateActiveObservation(attributes: LangfuseSpanAttributes, options?: {
1733
+ asType: "span";
1734
+ }): void;
1735
+ declare function updateActiveObservation(attributes: LangfuseGenerationAttributes, options: {
1736
+ asType: "generation";
1737
+ }): void;
1738
+ declare function updateActiveObservation(attributes: LangfuseAgentAttributes, options: {
1739
+ asType: "agent";
1740
+ }): void;
1741
+ declare function updateActiveObservation(attributes: LangfuseToolAttributes, options: {
1742
+ asType: "tool";
1743
+ }): void;
1744
+ declare function updateActiveObservation(attributes: LangfuseChainAttributes, options: {
1745
+ asType: "chain";
1746
+ }): void;
1747
+ declare function updateActiveObservation(attributes: LangfuseEmbeddingAttributes, options: {
1748
+ asType: "embedding";
1749
+ }): void;
1750
+ declare function updateActiveObservation(attributes: LangfuseEvaluatorAttributes, options: {
1751
+ asType: "evaluator";
1752
+ }): void;
1753
+ declare function updateActiveObservation(attributes: LangfuseGuardrailAttributes, options: {
1754
+ asType: "guardrail";
1755
+ }): void;
1756
+ declare function updateActiveObservation(attributes: LangfuseRetrieverAttributes, options: {
1757
+ asType: "retriever";
1758
+ }): void;
1610
1759
  /**
1611
1760
  * Options for the observe decorator function.
1612
1761
  *
@@ -1875,7 +2024,7 @@ interface ObserveOptions {
1875
2024
  *
1876
2025
  * @public
1877
2026
  */
1878
- declare function observe<T extends (...args: unknown[]) => unknown>(fn: T, options?: ObserveOptions): T;
2027
+ declare function observe<T extends (...args: any[]) => any>(fn: T, options?: ObserveOptions): T;
1879
2028
  /**
1880
2029
  * Creates a trace ID for OpenTelemetry spans.
1881
2030
  *
package/dist/index.d.ts CHANGED
@@ -1598,15 +1598,164 @@ declare function startActiveObservation<F extends (span: LangfuseSpan) => unknow
1598
1598
  * @public
1599
1599
  */
1600
1600
  declare function updateActiveTrace(attributes: LangfuseTraceAttributes): void;
1601
- declare function updateActiveObservation(currentType: "span", attributes: LangfuseSpanAttributes): void;
1602
- declare function updateActiveObservation(currentType: "generation", attributes: LangfuseGenerationAttributes): void;
1603
- declare function updateActiveObservation(currentType: "agent", attributes: LangfuseAgentAttributes): void;
1604
- declare function updateActiveObservation(currentType: "tool", attributes: LangfuseToolAttributes): void;
1605
- declare function updateActiveObservation(currentType: "chain", attributes: LangfuseChainAttributes): void;
1606
- declare function updateActiveObservation(currentType: "embedding", attributes: LangfuseEmbeddingAttributes): void;
1607
- declare function updateActiveObservation(currentType: "evaluator", attributes: LangfuseEvaluatorAttributes): void;
1608
- declare function updateActiveObservation(currentType: "guardrail", attributes: LangfuseGuardrailAttributes): void;
1609
- declare function updateActiveObservation(currentType: "retriever", attributes: LangfuseRetrieverAttributes): void;
1601
+ /**
1602
+ * Updates the currently active observation with new attributes.
1603
+ *
1604
+ * This function finds the currently active OpenTelemetry span in the execution context
1605
+ * and updates it with Langfuse-specific attributes. It supports all observation types
1606
+ * through TypeScript overloads, providing type safety for attributes based on the
1607
+ * specified `asType` parameter. If no active span exists, the update is skipped with a warning.
1608
+ *
1609
+ * ## Type Safety
1610
+ * - Automatic type inference based on `asType` parameter
1611
+ * - Compile-time validation of attribute compatibility
1612
+ * - IntelliSense support for observation-specific attributes
1613
+ *
1614
+ * ## Context Requirements
1615
+ * - Must be called within an active OpenTelemetry span context
1616
+ * - Typically used inside `startActiveObservation` callbacks or manual span contexts
1617
+ * - Relies on OpenTelemetry's context propagation mechanism
1618
+ *
1619
+ * ## Supported Observation Types
1620
+ * - **span** (default): General-purpose operations and workflows
1621
+ * - **generation**: LLM calls and AI model interactions
1622
+ * - **agent**: AI agent workflows with tool usage
1623
+ * - **tool**: Individual tool calls and API requests
1624
+ * - **chain**: Multi-step processes and pipelines
1625
+ * - **retriever**: Document retrieval and search operations
1626
+ * - **evaluator**: Quality assessment and scoring
1627
+ * - **guardrail**: Safety checks and content filtering
1628
+ * - **embedding**: Text embedding and vector operations
1629
+ *
1630
+ * @param attributes - Observation-specific attributes to update (type varies by observation type)
1631
+ * @param options - Configuration specifying observation type (defaults to 'span')
1632
+ *
1633
+ * @example
1634
+ * ```typescript
1635
+ * import { updateActiveObservation, startActiveObservation } from '@langfuse/tracing';
1636
+ *
1637
+ * // Update active span (default)
1638
+ * await startActiveObservation('data-processing', async (observation) => {
1639
+ * // Process data...
1640
+ * const result = await processData(inputData);
1641
+ *
1642
+ * // Update with results
1643
+ * updateActiveObservation({
1644
+ * output: { processedRecords: result.count },
1645
+ * metadata: { processingTime: result.duration }
1646
+ * });
1647
+ * });
1648
+ *
1649
+ * // Update active generation
1650
+ * await startActiveObservation('llm-call', async () => {
1651
+ * const response = await openai.chat.completions.create({
1652
+ * model: 'gpt-4',
1653
+ * messages: [{ role: 'user', content: 'Hello' }]
1654
+ * });
1655
+ *
1656
+ * // Update with LLM-specific attributes
1657
+ * updateActiveObservation({
1658
+ * output: response.choices[0].message,
1659
+ * usageDetails: {
1660
+ * promptTokens: response.usage.prompt_tokens,
1661
+ * completionTokens: response.usage.completion_tokens,
1662
+ * totalTokens: response.usage.total_tokens
1663
+ * },
1664
+ * costDetails: {
1665
+ * totalCost: 0.025,
1666
+ * currency: 'USD'
1667
+ * }
1668
+ * }, { asType: 'generation' });
1669
+ * }, {}, { asType: 'generation' });
1670
+ *
1671
+ * // Update active tool execution
1672
+ * await startActiveObservation('web-search', async () => {
1673
+ * const results = await searchAPI('latest news');
1674
+ *
1675
+ * updateActiveObservation({
1676
+ * output: {
1677
+ * results: results,
1678
+ * count: results.length,
1679
+ * relevanceScore: 0.89
1680
+ * },
1681
+ * metadata: {
1682
+ * searchLatency: 150,
1683
+ * cacheHit: false
1684
+ * }
1685
+ * }, { asType: 'tool' });
1686
+ * }, {}, { asType: 'tool' });
1687
+ *
1688
+ * // Update active agent workflow
1689
+ * await startActiveObservation('research-agent', async () => {
1690
+ * // Agent performs multiple operations...
1691
+ * const findings = await conductResearch();
1692
+ *
1693
+ * updateActiveObservation({
1694
+ * output: {
1695
+ * completed: true,
1696
+ * toolsUsed: ['web-search', 'summarizer'],
1697
+ * iterationsRequired: 3,
1698
+ * confidence: 0.92
1699
+ * },
1700
+ * metadata: {
1701
+ * efficiency: 0.85,
1702
+ * qualityScore: 0.88
1703
+ * }
1704
+ * }, { asType: 'agent' });
1705
+ * }, {}, { asType: 'agent' });
1706
+ *
1707
+ * // Update active chain workflow
1708
+ * await startActiveObservation('rag-pipeline', async () => {
1709
+ * // Execute multi-step RAG process...
1710
+ * const finalResponse = await executeRAGPipeline();
1711
+ *
1712
+ * updateActiveObservation({
1713
+ * output: {
1714
+ * finalResponse: finalResponse,
1715
+ * stepsCompleted: 4,
1716
+ * documentsRetrieved: 8,
1717
+ * qualityScore: 0.91
1718
+ * },
1719
+ * metadata: {
1720
+ * pipelineEfficiency: 0.87,
1721
+ * totalLatency: 3200
1722
+ * }
1723
+ * }, { asType: 'chain' });
1724
+ * }, {}, { asType: 'chain' });
1725
+ * ```
1726
+ *
1727
+ * @see {@link startActiveObservation} - For creating active observation contexts
1728
+ * @see {@link updateActiveTrace} - For updating trace-level attributes
1729
+ *
1730
+ * @public
1731
+ */
1732
+ declare function updateActiveObservation(attributes: LangfuseSpanAttributes, options?: {
1733
+ asType: "span";
1734
+ }): void;
1735
+ declare function updateActiveObservation(attributes: LangfuseGenerationAttributes, options: {
1736
+ asType: "generation";
1737
+ }): void;
1738
+ declare function updateActiveObservation(attributes: LangfuseAgentAttributes, options: {
1739
+ asType: "agent";
1740
+ }): void;
1741
+ declare function updateActiveObservation(attributes: LangfuseToolAttributes, options: {
1742
+ asType: "tool";
1743
+ }): void;
1744
+ declare function updateActiveObservation(attributes: LangfuseChainAttributes, options: {
1745
+ asType: "chain";
1746
+ }): void;
1747
+ declare function updateActiveObservation(attributes: LangfuseEmbeddingAttributes, options: {
1748
+ asType: "embedding";
1749
+ }): void;
1750
+ declare function updateActiveObservation(attributes: LangfuseEvaluatorAttributes, options: {
1751
+ asType: "evaluator";
1752
+ }): void;
1753
+ declare function updateActiveObservation(attributes: LangfuseGuardrailAttributes, options: {
1754
+ asType: "guardrail";
1755
+ }): void;
1756
+ declare function updateActiveObservation(attributes: LangfuseRetrieverAttributes, options: {
1757
+ asType: "retriever";
1758
+ }): void;
1610
1759
  /**
1611
1760
  * Options for the observe decorator function.
1612
1761
  *
@@ -1875,7 +2024,7 @@ interface ObserveOptions {
1875
2024
  *
1876
2025
  * @public
1877
2026
  */
1878
- declare function observe<T extends (...args: unknown[]) => unknown>(fn: T, options?: ObserveOptions): T;
2027
+ declare function observe<T extends (...args: any[]) => any>(fn: T, options?: ObserveOptions): T;
1879
2028
  /**
1880
2029
  * Creates a trace ID for OpenTelemetry spans.
1881
2030
  *
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import { getGlobalLogger as getGlobalLogger2 } from "@langfuse/core";
2
+ import { getGlobalLogger as getGlobalLogger2, LangfuseOtelSpanAttributes as LangfuseOtelSpanAttributes2 } from "@langfuse/core";
3
3
  import {
4
4
  trace as trace2,
5
5
  context,
@@ -411,7 +411,7 @@ var LangfuseEvent = class extends LangfuseBaseObservation {
411
411
  };
412
412
 
413
413
  // src/index.ts
414
- import { LangfuseOtelSpanAttributes as LangfuseOtelSpanAttributes2 } from "@langfuse/core";
414
+ import { LangfuseOtelSpanAttributes as LangfuseOtelSpanAttributes3 } from "@langfuse/core";
415
415
  function createOtelSpan(params) {
416
416
  return getLangfuseTracer().startSpan(
417
417
  params.name,
@@ -609,7 +609,8 @@ function updateActiveTrace(attributes) {
609
609
  }
610
610
  span.setAttributes(createTraceAttributes(attributes));
611
611
  }
612
- function updateActiveObservation(currentType, attributes) {
612
+ function updateActiveObservation(attributes, options) {
613
+ var _a;
613
614
  const span = trace2.getActiveSpan();
614
615
  if (!span) {
615
616
  getGlobalLogger2().warn(
@@ -617,7 +618,14 @@ function updateActiveObservation(currentType, attributes) {
617
618
  );
618
619
  return;
619
620
  }
620
- span.setAttributes(createObservationAttributes(currentType, attributes));
621
+ const otelAttributes = createObservationAttributes(
622
+ (_a = options == null ? void 0 : options.asType) != null ? _a : "span",
623
+ attributes
624
+ );
625
+ if (!(options == null ? void 0 : options.asType)) {
626
+ otelAttributes[LangfuseOtelSpanAttributes2.OBSERVATION_TYPE] = void 0;
627
+ }
628
+ span.setAttributes(otelAttributes);
621
629
  }
622
630
  function observe(fn, options = {}) {
623
631
  const {
@@ -734,7 +742,7 @@ export {
734
742
  LangfuseEvent,
735
743
  LangfuseGeneration,
736
744
  LangfuseGuardrail,
737
- LangfuseOtelSpanAttributes2 as LangfuseOtelSpanAttributes,
745
+ LangfuseOtelSpanAttributes3 as LangfuseOtelSpanAttributes,
738
746
  LangfuseRetriever,
739
747
  LangfuseSpan,
740
748
  LangfuseTool,