Skip to content

feat(langchain): emit GenAI request attributes - #213

Draft
Jackson Weber (JacksonWeber) wants to merge 8 commits into
microsoft:mainfrom
JacksonWeber:jacksonweber/langchain-genai-request-attributes
Draft

feat(langchain): emit GenAI request attributes#213
Jackson Weber (JacksonWeber) wants to merge 8 commits into
microsoft:mainfrom
JacksonWeber:jacksonweber/langchain-genai-request-attributes

Conversation

@JacksonWeber

@JacksonWeber Jackson Weber (JacksonWeber) commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • emit available GenAI request parameters from LangChain extra.invocation_params on chat spans
  • derive gen_ai.output.type from explicit output types and structured response formats
  • support OpenAI-style snake_case and provider/LangChain camelCase aliases with semconv-safe type validation

Attributes

  • gen_ai.output.type
  • gen_ai.request.temperature
  • gen_ai.request.top_p / gen_ai.request.top_k
  • gen_ai.request.max_tokens
  • frequency/presence penalties, seed, stop sequences, and stream

Validation

  • LangChain unit, tracer, and functional suites pass
  • A365 exporter and span-processor suites pass
  • lint, formatting, and build pass
  • live Azure OpenAI invocation validated against Application Insights using @langchain/openai 1.5.5 and @langchain/core 1.2.3; the ingested span retained expected typed values for temperature, top-p, max tokens, penalties, seed, stop sequences, stream, and JSON output type

A365 compatibility

  • the Agent365 exporter copies span attributes into its OTLP-like payload without renaming or coercion
  • added a regression test proving the complete new attribute set retains its string, number, boolean, and string-array types in the A365 payload
  • gen_ai.request.temperature, gen_ai.request.top_p, and gen_ai.request.max_tokens are already defined by the upstream Agent365 JS constants
  • all added keys and types follow the current OpenTelemetry GenAI semantic conventions; A365 identity partitioning and message truncation behavior are unchanged

Upstream assessment

Current LangChain.js exposes these values as raw callback invocation_params but does not map them to OpenTelemetry gen_ai.* attributes, so the distro remains the appropriate implementation layer.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@JacksonWeber
Jackson Weber (JacksonWeber) force-pushed the jacksonweber/langchain-genai-request-attributes branch from eb3eb7d to 02f1d14 Compare August 6, 2026 17:41
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Adds LangChain instrumentation support for emitting GenAI request-related OpenTelemetry attributes (including output type) from extra.invocation_params, with validation/normalization and expanded unit test coverage.

Changes:

  • Introduces new GenAI semantic convention constants for request parameters and gen_ai.output.type.
  • Adds setRequestAttributes to LangChain utils and wires it into the LangChain tracer.
  • Expands unit tests to cover mapping/normalization and exporter payload preservation.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/internal/unit/genai/langchain/utils.test.ts Adds focused unit tests for setRequestAttributes mapping/validation behavior.
test/internal/unit/genai/langchain/tracer.test.ts Verifies tracer emits request attributes/output type onto spans.
test/internal/unit/a365/agent365Exporter.test.ts Ensures A365 exporter payload preserves new GenAI attributes.
src/genai/semconv.ts Defines new gen_ai.* attribute constants used by instrumentation.
src/genai/instrumentations/langchain/utils.ts Implements request attribute extraction, normalization, and type validation.
src/genai/instrumentations/langchain/tracer.ts Hooks request attribute setting into span creation flow.
CHANGELOG.md Documents the new instrumentation feature under Unreleased.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/genai/instrumentations/langchain/utils.ts
Comment thread src/genai/instrumentations/langchain/utils.ts Outdated
Comment thread src/genai/instrumentations/langchain/utils.ts
Comment thread src/genai/instrumentations/langchain/utils.ts Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rads-1996

Copy link
Copy Markdown
Member
image

where did the other checks go? Is it a github issue?

Comment thread CHANGELOG.md Outdated
## [Unreleased]

### Features Added
- Emit `gen_ai.output.type` and available GenAI request parameters on LangChain chat spans.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link to the PR is missing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link is added and it looks like the PR change fixed the other CI jobs not running as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Only 4 jobs running, I don't think the entire list is being fetched.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that is comperehensive from checking the pr-validation.yml We just have an ES-lint and testing the two LTS versions of Node.js (22 & 24) that we currently support. Although this might not be a bad time to add a Node 26 check as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, looks like 26 is current, not LTS yet.

@JacksonWeber

Copy link
Copy Markdown
Contributor Author
image where did the other checks go? Is it a github issue?

Interesting, yeah. There were issues with github actions yesterday. Let me fix this one.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread src/genai/instrumentations/langchain/utils.ts Outdated
speech: "speech",
};

function normalizeOutputType(value: unknown): string | undefined {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are none of these helper functions available upstream which we could reuse here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LangChain doesn’t provide public helpers for normalizing these provider-specific invocation parameters and OpenTelemetry only validates values after they’ve been normalized, so we still need the small local coercion helpers here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how does opentelemetry (genai js) normalize the values? When I mentioned upstream in my original comment, I was referring to the upstream langchain instrumentation, sorry for the confusion.

@JacksonWeber Jackson Weber (JacksonWeber) Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. The upstream otel langchain instrumentation doesn't currently emit these attributes, so it doesn't have any normalization logic for them.

Makes sense that we can try to implement these upstream there instead of only in our vendored implementation. But we don't consume that package anywhere in this project, so is the expectation that customers just pull the upstream OTel JS langchain instrumentation to use alongside this distro?

return strings.length === value.length && strings.length > 0 ? strings : undefined;
}

const OUTPUT_TYPES: Readonly<Record<string, string>> = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per semconv, this is supported list of output types - gen_ai.output.type. Are we planning to support these extras in the distro only? Also, you are missing the image type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the list to support the image type and unify with what's described in sem conv. Does it make sense to support these in our vendored langchain instrumentation for now and then work on supporting them upstream in the future?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest the other way around, first upstream (semconv repo) and then support them in the distro. Are there any specific scenarios where you would want to add support for the types not listed in semconv?

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
const seed = integer(firstDefined(params, ["seed"]));
if (seed !== undefined) span.setAttribute(ATTR_GEN_AI_REQUEST_SEED, seed);

const topK = integer(firstDefined(params, ["top_k", "topK"]));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little confused why are we supporting the camelCase, shouldn't the attributes satisfy the _ convention?

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Match upstream OpenTelemetry GenAI instrumentations by accepting typed provider parameters without coercing numeric or boolean strings.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@JacksonWeber
Jackson Weber (JacksonWeber) marked this pull request as draft August 8, 2026 22:22
@JacksonWeber
Jackson Weber (JacksonWeber) marked this pull request as ready for review August 8, 2026 22:30
@JacksonWeber
Jackson Weber (JacksonWeber) marked this pull request as draft August 8, 2026 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants