Repository navigation
Add opt-in completion metadata and harden prompt delimiters - #336
Conversation
There was a problem hiding this comment.
Copilot review overview
Review tier: Lite
Findings: 1
New issues introduced by this change (2)
| Severity | Finding |
|---|---|
src/typechat/LanguageModel.cs — When OpenAIConfig.UseResponsesApi is explicitly set to false, non-Azure configuration should ensure… |
|
src/typechat/HttpEx.cs — GetJsonResponseWithRawAsync reuses a single StringContent across retries, but… |
What changed in this PR
This PR upgrades TypeChat’s language-model abstraction to return a richer LanguageModelResponse (text + normalized completion metadata), adds normalized token/finish-reason types, updates prompts to JSON-encode user/error sections to reduce prompt-injection risk, and expands unit tests to cover the new behaviors (including OpenAI “Responses API” routing).
Changes:
- Changed
ILanguageModel.CompleteAsync(and implementations) to returnLanguageModelResponse, carryingCompletionInfo(model, finish reason, token usage, raw JSON). - Added non-throwing
JsonTranslator.TranslateToResultAsyncAPIs returningResult<T>with attached completion metadata on success. - Added HTTP helper to capture raw JSON responses and updated prompt templates to JSON-string encode untrusted request/error text.
| File | Description |
|---|---|
| tests/TypeChat.UnitTests/TestLanguageModel.cs | Adds coverage for CompletionInfo parsing and /responses endpoint routing. |
| tests/TypeChat.UnitTests/TestJsonTranslatorPrompts.cs | Verifies prompt sections JSON-encode untrusted request/error text. |
| tests/TypeChat.TestLib/Models.cs | Updates mock language model to new LanguageModelResponse return type. |
| tests/TypeChat.IntegrationTests/TestEndToEnd.cs | Adjusts assertion to handle LanguageModelResponse (string conversion). |
| src/typechat/TokenUsage.cs | Introduces normalized token usage model. |
| src/typechat/Result.cs | Adds optional CompletionInfo to successful results. |
| src/typechat/OpenAIConfig.cs | Adds UseResponsesApi selector for endpoint variant selection. |
| src/typechat/LanguageModelResponse.cs | New response wrapper with implicit conversion to/from string. |
| src/typechat/LanguageModel.cs | Adds Responses API support, endpoint inference/rewrites, and completion metadata extraction. |
| src/typechat/JsonTranslatorPrompts.cs | Switches prompt embedding to JSON-string encoding for safety/consistency. |
| src/typechat/JsonTranslator.cs | Adds non-throwing translation APIs and propagates completion info + repair attempts. |
| src/typechat/ILanguageModel.cs | Interface updated to return LanguageModelResponse. |
| src/typechat/HttpEx.cs | Adds GetJsonResponseWithRawAsync to return parsed + raw response JSON. |
| src/typechat/CompletionInfo.cs | New normalized completion metadata container (model/usage/finish/raw/repairs). |
| src/typechat/CompletionFinishReason.cs | New normalized finish reason enum. |
| src/typechat.sk/TextCompletionModel.cs | Updates SK adapter to return LanguageModelResponse. |
| src/typechat.sk/ChatLanguageModel.cs | Updates SK adapter to return LanguageModelResponse. |
| src/typechat.meai/ChatLanguageModel.cs | Updates MEAI adapter to return LanguageModelResponse with normalized metadata. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Preserve the original completion API and translator hooks while adding optional metadata and validation-result APIs. Retain failure context from main, fix endpoint and output handling, and test consumers compiled against the original contracts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Copilot resolve the merge conflicts in this pull request |
…k_fix # Conflicts: # src/typechat/LanguageModel.cs # src/typechat/OpenAIConfig.cs # tests/TypeChat.TestLib/MockHttp.cs # tests/TypeChat.UnitTests/TypeChat.UnitTests.csproj Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Resolved in merge commit |


Summary
main(d0b9597), including Retain rejected output and repair failure context #338's rejected-output and repair-failure context.ILanguageModel.CompleteAsyncand all existing provider methods asTask<string>. Add opt-inILanguageModelWithMetadata.CompleteWithMetadataAsyncplus an extension method that supports existing text-only models.TranslateAsynccalls routed through the original protectedGetResponseAsynchook. The additiveTranslateToResultAsyncAPI uses a separate metadata-aware hook and retains both completion metadata and failure context.Compatibility
Existing model implementations,
Task<string>callers, and translator subclasses continue to work without recompilation. REST and Microsoft.Extensions.AI expose the optional metadata capability. Semantic Kernel and other text-only models work through the extension-method fallback with unavailable metadata left null. No shared "last completion" state is introduced.The original throwing translation API remains supported. Subclasses opting into the result-returning API can override
GetResponseWithMetadataAsync.Validation
NU1900warning for an unreachable NuGet audit feed.