Skip to content

Remove openai latest dep restriction #14423

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

Merged
merged 4 commits into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/supported-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ These are the supported libraries and frameworks:
| [MongoDB Driver](https://mongodb.github.io/mongo-java-driver/) | 3.1+ | [opentelemetry-mongo-3.1](../instrumentation/mongo/mongo-3.1/library) | [Database Client Spans], [Database Client Metrics] [6] |
| [MyBatis](https://mybatis.org/mybatis-3/) | 3.2+ | N/A | none |
| [Netty HTTP codec [5]](https://github.com/netty/netty) | 3.8+ | [opentelemetry-netty-4.1](../instrumentation/netty/netty-4.1/library) | [HTTP Client Spans], [HTTP Client Metrics], [HTTP Server Spans], [HTTP Server Metrics] |
| [OpenAI Java SDK](https://github.com/openai/openai-java) | 1.1+ (not including 3.0+ yet) | [openai-java-1.1](../instrumentation/openai/openai-java-1.1/library) | [GenAI Client Spans], [GenAI Client Metrics] |
| [OpenAI Java SDK](https://github.com/openai/openai-java) | 1.1+ | [openai-java-1.1](../instrumentation/openai/openai-java-1.1/library) | [GenAI Client Spans], [GenAI Client Metrics] |
| [OpenSearch Rest Client](https://github.com/opensearch-project/opensearch-java) | 1.0+ | | [Database Client Spans], [Database Client Metrics] [6] |
| [OkHttp](https://github.com/square/okhttp/) | 2.2+ | [opentelemetry-okhttp-3.0](../instrumentation/okhttp/okhttp-3.0/library) | [HTTP Client Spans], [HTTP Client Metrics] |
| [Oracle UCP](https://docs.oracle.com/database/121/JJUCP/) | 11.2+ | [opentelemetry-oracle-ucp-11.2](../instrumentation/oracle-ucp-11.2/library) | [Database Pool Metrics] |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ dependencies {
library("com.openai:openai-java:1.1.0")

testImplementation(project(":instrumentation:openai:openai-java-1.1:testing"))

latestDepTestLibrary("com.openai:openai-java:2.+") // documented limitation
}

tasks {
Expand Down
1 change: 0 additions & 1 deletion instrumentation/openai/openai-java-1.1/library/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Library Instrumentation for OpenAI Java SDK version 1.1.0 and higher

Provides OpenTelemetry instrumentation for [openai-java](https://github.com/openai/openai-java/).
Versions 1.1 through 2.x are supported.

## Quickstart

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ dependencies {
library("com.openai:openai-java:1.1.0")

testImplementation(project(":instrumentation:openai:openai-java-1.1:testing"))

latestDepTestLibrary("com.openai:openai-java:2.+") // documented limitation
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@
import io.opentelemetry.api.logs.LogRecordBuilder;
import io.opentelemetry.api.logs.Logger;
import io.opentelemetry.context.Context;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

final class ChatCompletionEventsHelper {

Expand Down Expand Up @@ -215,21 +220,232 @@ private static LogRecordBuilder newEvent(Logger eventLogger, String name) {
private static Value<?> buildToolCallEventObject(
ChatCompletionMessageToolCall call, boolean captureMessageContent) {
Map<String, Value<?>> result = new HashMap<>();
result.put("id", Value.of(call.id()));
result.put("type", Value.of("function")); // "function" is the only currently supported type
result.put("function", buildFunctionEventObject(call.function(), captureMessageContent));
FunctionAccess functionAccess = getFunctionAccess(call);
if (functionAccess != null) {
result.put("id", Value.of(functionAccess.id()));
result.put("type", Value.of("function")); // "function" is the only currently supported type
result.put("function", buildFunctionEventObject(functionAccess, captureMessageContent));
}
return Value.of(result);
}

private static Value<?> buildFunctionEventObject(
ChatCompletionMessageToolCall.Function function, boolean captureMessageContent) {
FunctionAccess functionAccess, boolean captureMessageContent) {
Map<String, Value<?>> result = new HashMap<>();
result.put("name", Value.of(function.name()));
result.put("name", Value.of(functionAccess.name()));
if (captureMessageContent) {
result.put("arguments", Value.of(function.arguments()));
result.put("arguments", Value.of(functionAccess.arguments()));
}
return Value.of(result);
}

@Nullable
private static FunctionAccess getFunctionAccess(ChatCompletionMessageToolCall call) {
if (V1FunctionAccess.isAvailable()) {
return V1FunctionAccess.create(call);
}
if (V3FunctionAccess.isAvailable()) {
return V3FunctionAccess.create(call);
}

return null;
}

private interface FunctionAccess {
String id();

String name();

String arguments();
}

private static String invokeStringHandle(@Nullable MethodHandle methodHandle, Object object) {
if (methodHandle == null) {
return "";
}

try {
return (String) methodHandle.invoke(object);
} catch (Throwable ignore) {
return "";
}
}

private static class V1FunctionAccess implements FunctionAccess {
@Nullable private static final MethodHandle idHandle;
@Nullable private static final MethodHandle functionHandle;
@Nullable private static final MethodHandle nameHandle;
@Nullable private static final MethodHandle argumentsHandle;

static {
MethodHandle id;
MethodHandle function;
MethodHandle name;
MethodHandle arguments;

try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
id =
lookup.findVirtual(
ChatCompletionMessageToolCall.class, "id", MethodType.methodType(String.class));
Class<?> functionClass =
Class.forName(
"com.openai.models.chat.completions.ChatCompletionMessageToolCall$Function");
function =
lookup.findVirtual(
ChatCompletionMessageToolCall.class,
"function",
MethodType.methodType(functionClass));
name = lookup.findVirtual(functionClass, "name", MethodType.methodType(String.class));
arguments =
lookup.findVirtual(functionClass, "arguments", MethodType.methodType(String.class));
} catch (Exception exception) {
id = null;
function = null;
name = null;
arguments = null;
}
idHandle = id;
functionHandle = function;
nameHandle = name;
argumentsHandle = arguments;
}

private final ChatCompletionMessageToolCall toolCall;
private final Object function;

V1FunctionAccess(ChatCompletionMessageToolCall toolCall, Object function) {
this.toolCall = toolCall;
this.function = function;
}

@Nullable
static FunctionAccess create(ChatCompletionMessageToolCall toolCall) {
if (functionHandle == null) {
return null;
}

try {
return new V1FunctionAccess(toolCall, functionHandle.invoke(toolCall));
} catch (Throwable ignore) {
return null;
}
}

static boolean isAvailable() {
return idHandle != null;
}

@Override
public String id() {
return invokeStringHandle(idHandle, toolCall);
}

@Override
public String name() {
return invokeStringHandle(nameHandle, function);
}

@Override
public String arguments() {
return invokeStringHandle(argumentsHandle, function);
}
}

static class V3FunctionAccess implements FunctionAccess {
@Nullable private static final MethodHandle functionToolCallHandle;
@Nullable private static final MethodHandle idHandle;
@Nullable private static final MethodHandle functionHandle;
@Nullable private static final MethodHandle nameHandle;
@Nullable private static final MethodHandle argumentsHandle;

static {
MethodHandle functionToolCall;
MethodHandle id;
MethodHandle function;
MethodHandle name;
MethodHandle arguments;

try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
functionToolCall =
lookup.findVirtual(
ChatCompletionMessageToolCall.class,
"function",
MethodType.methodType(Optional.class));
Class<?> functionToolCallClass =
Class.forName(
"com.openai.models.chat.completions.ChatCompletionMessageFunctionToolCall");
id = lookup.findVirtual(functionToolCallClass, "id", MethodType.methodType(String.class));
Class<?> functionClass =
Class.forName(
"com.openai.models.chat.completions.ChatCompletionMessageFunctionToolCall$Function");
function =
lookup.findVirtual(
functionToolCallClass, "function", MethodType.methodType(functionClass));
name = lookup.findVirtual(functionClass, "name", MethodType.methodType(String.class));
arguments =
lookup.findVirtual(functionClass, "arguments", MethodType.methodType(String.class));
} catch (Exception exception) {
functionToolCall = null;
id = null;
function = null;
name = null;
arguments = null;
}
functionToolCallHandle = functionToolCall;
idHandle = id;
functionHandle = function;
nameHandle = name;
argumentsHandle = arguments;
}

private final Object functionToolCall;
private final Object function;

V3FunctionAccess(Object functionToolCall, Object function) {
this.functionToolCall = functionToolCall;
this.function = function;
}

@Nullable
@SuppressWarnings("unchecked")
static FunctionAccess create(ChatCompletionMessageToolCall toolCall) {
if (functionToolCallHandle == null || functionHandle == null) {
return null;
}

try {
Optional<Object> optional = (Optional<Object>) functionToolCallHandle.invoke(toolCall);
if (!optional.isPresent()) {
return null;
}
Object functionToolCall = optional.get();
return new V3FunctionAccess(functionToolCall, functionHandle.invoke(functionToolCall));
} catch (Throwable ignore) {
return null;
}
}

static boolean isAvailable() {
return idHandle != null;
}

@Override
public String id() {
return invokeStringHandle(idHandle, functionToolCall);
}

@Override
public String name() {
return invokeStringHandle(nameHandle, function);
}

@Override
public String arguments() {
return invokeStringHandle(argumentsHandle, function);
}
}

private ChatCompletionEventsHelper() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GenAiSystemIncubatingValues.OPENAI;
import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GenAiTokenTypeIncubatingValues.COMPLETION;
import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GenAiTokenTypeIncubatingValues.INPUT;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -318,14 +317,14 @@ void toolCallsNoCaptureContent() {
assertThat(toolCalls).hasSize(2);
String newYorkCallId =
toolCalls.stream()
.filter(call -> call.function().arguments().contains("New York"))
.map(ChatCompletionMessageToolCall::id)
.filter(call -> testHelper.arguments(call).contains("New York"))
.map(call -> testHelper.id(call))
.findFirst()
.get();
String londonCallId =
toolCalls.stream()
.filter(call -> call.function().arguments().contains("London"))
.map(ChatCompletionMessageToolCall::id)
.filter(call -> testHelper.arguments(call).contains("London"))
.map(call -> testHelper.id(call))
.findFirst()
.get();

Expand Down Expand Up @@ -774,63 +773,18 @@ void streamToolCallsNoCaptureContent() {
List<ChatCompletionChunk> chunks =
doCompletionsStreaming(params, clientNoCaptureContent(), clientAsyncNoCaptureContent());

List<ChatCompletionMessageToolCall> toolCalls = new ArrayList<>();

ChatCompletionMessageToolCall.Builder currentToolCall = null;
ChatCompletionMessageToolCall.Function.Builder currentFunction = null;
StringBuilder currentArgs = null;

for (ChatCompletionChunk chunk : chunks) {
List<ChatCompletionChunk.Choice.Delta.ToolCall> calls =
chunk.choices().get(0).delta().toolCalls().orElse(emptyList());
if (calls.isEmpty()) {
continue;
}
for (ChatCompletionChunk.Choice.Delta.ToolCall call : calls) {
if (call.id().isPresent()) {
if (currentToolCall != null) {
if (currentFunction != null && currentArgs != null) {
currentFunction.arguments(currentArgs.toString());
currentToolCall.function(currentFunction.build());
}
toolCalls.add(currentToolCall.build());
}
currentToolCall = ChatCompletionMessageToolCall.builder().id(call.id().get());
currentFunction = ChatCompletionMessageToolCall.Function.builder();
currentArgs = new StringBuilder();
}
if (call.function().isPresent()) {
if (call.function().get().name().isPresent()) {
if (currentFunction != null) {
currentFunction.name(call.function().get().name().get());
}
}
if (call.function().get().arguments().isPresent()) {
if (currentArgs != null) {
currentArgs.append(call.function().get().arguments().get());
}
}
}
}
}
if (currentToolCall != null) {
if (currentFunction != null && currentArgs != null) {
currentFunction.arguments(currentArgs.toString());
currentToolCall.function(currentFunction.build());
}
toolCalls.add(currentToolCall.build());
}
List<ChatCompletionMessageToolCall> toolCalls = getToolCalls(chunks);

String newYorkCallId =
toolCalls.stream()
.filter(call -> call.function().arguments().contains("New York"))
.map(ChatCompletionMessageToolCall::id)
.filter(call -> testHelper.arguments(call).contains("New York"))
.map(call -> testHelper.id(call))
.findFirst()
.get();
String londonCallId =
toolCalls.stream()
.filter(call -> call.function().arguments().contains("London"))
.map(ChatCompletionMessageToolCall::id)
.filter(call -> testHelper.arguments(call).contains("London"))
.map(call -> testHelper.id(call))
.findFirst()
.get();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
id("otel.java-conventions")
}

dependencies {
api(project(":testing-common"))
compileOnly("com.openai:openai-java:3.0.0")
}
Loading
Loading