feat: Add metadata field to request and response (#4237)

This changes adds Optional metadata field to OpenAI compatible request
and response object.

fixes: #3564

Signed-off-by: Abhishek Bongale <abhishekbongale@outlook.com>
Co-authored-by: Ashwin Bharambe <ashwin.bharambe@gmail.com>
This commit is contained in:
Abhishek Bongale 2025-12-01 18:48:53 +00:00 committed by GitHub
parent 28ff6d8659
commit 618c03405c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 98 additions and 0 deletions

View file

@ -109,6 +109,7 @@ class MetaReferenceAgentsImpl(Agents):
max_infer_iters: int | None = 10,
guardrails: list[ResponseGuardrail] | None = None,
max_tool_calls: int | None = None,
metadata: dict[str, str] | None = None,
) -> OpenAIResponseObject:
assert self.openai_responses_impl is not None, "OpenAI responses not initialized"
result = await self.openai_responses_impl.create_openai_response(
@ -128,6 +129,7 @@ class MetaReferenceAgentsImpl(Agents):
guardrails,
parallel_tool_calls,
max_tool_calls,
metadata,
)
return result # type: ignore[no-any-return]

View file

@ -336,6 +336,7 @@ class OpenAIResponsesImpl:
guardrails: list[str | ResponseGuardrailSpec] | None = None,
parallel_tool_calls: bool | None = None,
max_tool_calls: int | None = None,
metadata: dict[str, str] | None = None,
):
stream = bool(stream)
text = OpenAIResponseText(format=OpenAIResponseTextFormat(type="text")) if text is None else text
@ -390,6 +391,7 @@ class OpenAIResponsesImpl:
guardrail_ids=guardrail_ids,
parallel_tool_calls=parallel_tool_calls,
max_tool_calls=max_tool_calls,
metadata=metadata,
)
if stream:
@ -442,6 +444,7 @@ class OpenAIResponsesImpl:
guardrail_ids: list[str] | None = None,
parallel_tool_calls: bool | None = True,
max_tool_calls: int | None = None,
metadata: dict[str, str] | None = None,
) -> AsyncIterator[OpenAIResponseObjectStream]:
# These should never be None when called from create_openai_response (which sets defaults)
# but we assert here to help mypy understand the types
@ -490,6 +493,7 @@ class OpenAIResponsesImpl:
guardrail_ids=guardrail_ids,
instructions=instructions,
max_tool_calls=max_tool_calls,
metadata=metadata,
)
# Stream the response

View file

@ -120,6 +120,7 @@ class StreamingResponseOrchestrator:
prompt: OpenAIResponsePrompt | None = None,
parallel_tool_calls: bool | None = None,
max_tool_calls: int | None = None,
metadata: dict[str, str] | None = None,
):
self.inference_api = inference_api
self.ctx = ctx
@ -137,6 +138,7 @@ class StreamingResponseOrchestrator:
self.parallel_tool_calls = parallel_tool_calls
# Max number of total calls to built-in tools that can be processed in a response
self.max_tool_calls = max_tool_calls
self.metadata = metadata
self.sequence_number = 0
# Store MCP tool mapping that gets built during tool processing
self.mcp_tool_to_server: dict[str, OpenAIResponseInputToolMCP] = (
@ -164,6 +166,7 @@ class StreamingResponseOrchestrator:
model=self.ctx.model,
status="completed",
output=[OpenAIResponseMessage(role="assistant", content=[refusal_content], type="message")],
metadata=self.metadata,
)
return OpenAIResponseObjectStreamResponseCompleted(response=refusal_response)
@ -199,6 +202,7 @@ class StreamingResponseOrchestrator:
prompt=self.prompt,
parallel_tool_calls=self.parallel_tool_calls,
max_tool_calls=self.max_tool_calls,
metadata=self.metadata,
)
async def create_response(self) -> AsyncIterator[OpenAIResponseObjectStream]: