Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ com.google.j2objc:j2objc-annotations:2.8=annotationProcessor,latestDepTestAnnota
com.google.j2objc:j2objc-annotations:3.1=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.google.re2j:re2j:1.8=latestDepTestRuntimeClasspath,testRuntimeClasspath
com.openai:openai-java-client-okhttp:3.0.1=compileClasspath,testCompileClasspath,testRuntimeClasspath
com.openai:openai-java-client-okhttp:4.52.0=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath
com.openai:openai-java-client-okhttp:4.54.0=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath
com.openai:openai-java-core:3.0.1=compileClasspath,testCompileClasspath,testRuntimeClasspath
com.openai:openai-java-core:4.52.0=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath
com.openai:openai-java-core:4.54.0=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath
com.openai:openai-java:3.0.1=compileClasspath,testCompileClasspath,testRuntimeClasspath
com.openai:openai-java:4.52.0=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath
com.openai:openai-java:4.54.0=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath
com.squareup.moshi:moshi:1.11.0=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.squareup.okhttp3:logging-interceptor:3.12.12=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath
com.squareup.okhttp3:logging-interceptor:4.12.0=testRuntimeClasspath
Expand Down Expand Up @@ -118,7 +118,8 @@ org.hamcrest:hamcrest-core:1.3=latestDepTestRuntimeClasspath,testRuntimeClasspat
org.hamcrest:hamcrest:3.0=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.jctools:jctools-core-jdk11:4.0.6=latestDepTestRuntimeClasspath,testRuntimeClasspath
org.jctools:jctools-core:4.0.6=latestDepTestRuntimeClasspath,testRuntimeClasspath
org.jetbrains.kotlin:kotlin-reflect:1.8.10=latestDepTestRuntimeClasspath,testRuntimeClasspath
org.jetbrains.kotlin:kotlin-reflect:1.8.10=testRuntimeClasspath
org.jetbrains.kotlin:kotlin-reflect:1.8.20=latestDepTestRuntimeClasspath
org.jetbrains.kotlin:kotlin-stdlib-common:1.8.0=compileClasspath,latestDepTestCompileClasspath,testCompileClasspath
org.jetbrains.kotlin:kotlin-stdlib-common:1.9.10=latestDepTestRuntimeClasspath,testRuntimeClasspath
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0=compileClasspath,latestDepTestCompileClasspath,testCompileClasspath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import com.openai.models.responses.ResponseInputItem;
import datadog.trace.util.MethodHandles;
import java.lang.invoke.MethodHandle;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Helper class to handle FunctionCallOutput.output() method changes between openai-java versions.
* Helper class to handle FunctionCallOutput method changes between openai-java versions.
*
* <p>In version 3.x: output() returns String In version 4.0+: output() returns Output)
* <ul>
* <li>Version 3.x: {@code output()} returns {@code String}.
* <li>Version 4.0+: {@code output()} returns {@code Output}.
* <li>Versions before 4.54: {@code callId()} returns {@code String}.
* <li>Version 4.54+: {@code callId()} returns {@code Optional<String>}.
* </ul>
*/
public class FunctionCallOutputExtractor {
private static final Logger log = LoggerFactory.getLogger(FunctionCallOutputExtractor.class);
Expand All @@ -19,11 +25,13 @@ public class FunctionCallOutputExtractor {
private static final MethodHandles METHOD_HANDLES =
new MethodHandles(FUNCTION_CALL_OUTPUT_CLASS.getClassLoader());

private static final MethodHandle CALL_ID_METHOD;
private static final MethodHandle OUTPUT_METHOD;
private static final MethodHandle IS_STRING_METHOD;
private static final MethodHandle AS_STRING_METHOD;

static {
CALL_ID_METHOD = METHOD_HANDLES.method(FUNCTION_CALL_OUTPUT_CLASS, "callId");
OUTPUT_METHOD = METHOD_HANDLES.method(FUNCTION_CALL_OUTPUT_CLASS, "output");

Class<?> outputClass = null;
Expand All @@ -46,6 +54,20 @@ public class FunctionCallOutputExtractor {
}
}

public static String getCallIdAsString(ResponseInputItem.FunctionCallOutput functionCallOutput) {
try {
Object callId = METHOD_HANDLES.invoke(CALL_ID_METHOD, functionCallOutput);
if (callId instanceof Optional) {
callId = ((Optional<?>) callId).orElse(null);
}
if (callId == null || callId instanceof String) {
return (String) callId;
}
} catch (Throwable ignored) {
}
return null;
}

public static String getOutputAsString(ResponseInputItem.FunctionCallOutput functionCallOutput) {
try {
Object output = METHOD_HANDLES.invoke(OUTPUT_METHOD, functionCallOutput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private LLMObs.LLMMessage extractInputItemMessage(ResponseInputItem item) {
Optional<ResponseInputItem.FunctionCallOutput> functionCallOutput = item.functionCallOutput();
if (functionCallOutput.isPresent()) {
ResponseInputItem.FunctionCallOutput output = functionCallOutput.get();
String callId = output.callId();
String callId = FunctionCallOutputExtractor.getCallIdAsString(output);
String result = FunctionCallOutputExtractor.getOutputAsString(output);
LLMObs.ToolResult toolResult =
LLMObs.ToolResult.from("", "function_call_output", callId, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class ResponseServiceTest extends OpenAiTest {
inputTags[1].toolCalls[0].arguments == [location: "San Francisco, CA"]
inputTags[2].toolResults.size() == 1
inputTags[2].toolResults[0].type == "function_call_output"
!inputTags.isEmpty()
inputTags[2].toolResults[0].toolId == "call_123"
inputTags[2].toolResults[0].result == '{"temperature": "72°F", "conditions": "sunny", "humidity": "65%"}'

where:
Expand Down
Loading
Loading