-
Notifications
You must be signed in to change notification settings - Fork 0
fix: OpenAI 분석 응답 스키마 실패 재시도 #45
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import json | ||
| import unittest | ||
| from datetime import date | ||
|
|
||
| from app.config import OpenAISettings | ||
| from app.schemas import NewsletterAnalysisRequest | ||
| from app.services.openai_adapter import OpenAIAdapterError, OpenAINewsletterAdapter | ||
|
|
||
|
|
||
| def _i18n(text: str) -> dict[str, str]: | ||
| return {"KO": text, "US": text, "ZH": text, "VI": text} | ||
|
|
||
|
|
||
| def _valid_response() -> dict: | ||
| return { | ||
| "title": "수영 실기교육 안내", | ||
| "titleI18n": _i18n("수영 실기교육 안내"), | ||
| "summary": "수영 실기교육 일정을 확인하세요.", | ||
| "items": [ | ||
| { | ||
| "type": "schedule", | ||
| "title": "수영 실기교육", | ||
| "titleI18n": _i18n("수영 실기교육"), | ||
| "selectedDateCandidate": { | ||
| "index": 0, | ||
| "candidateId": "dc_1", | ||
| "originalText": "2026. 6. 15", | ||
| "normalizedDate": "2026-06-15", | ||
| }, | ||
| "dateStatus": "confirmed", | ||
| "datetime": "2026-06-15", | ||
| "timezone": "Asia/Seoul", | ||
| "evidenceText": "2026. 6. 15 수영 실기교육", | ||
| "confidence": 0.9, | ||
| "needsUserConfirmation": False, | ||
| "confirmationQuestion": None, | ||
| "checklistItems": [ | ||
| { | ||
| "content": "수영복 준비하기", | ||
| "contentI18n": _i18n("수영복 준비하기"), | ||
| "detail": "수영복, 수영모, 물안경을 준비합니다.", | ||
| "detailI18n": _i18n("수영복, 수영모, 물안경을 준비합니다."), | ||
| } | ||
| ], | ||
| } | ||
| ], | ||
| "conversationTopics": [], | ||
| "meta": { | ||
| "mode": "openai", | ||
| "dateCandidateCount": 1, | ||
| "requiresLLMReview": False, | ||
| "outputLanguage": "KO", | ||
| "localizedOutput": True, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| class StubOpenAINewsletterAdapter(OpenAINewsletterAdapter): | ||
| def __init__(self, responses: list[dict]) -> None: | ||
| super().__init__( | ||
| OpenAISettings( | ||
| enabled=True, | ||
| api_key="test-key", | ||
| model="test-model", | ||
| base_url="https://example.test/v1", | ||
| timeout_seconds=1, | ||
| ) | ||
| ) | ||
| self.responses = responses | ||
| self.payloads: list[dict] = [] | ||
|
|
||
| def _post_json(self, path: str, payload: dict) -> dict: | ||
| self.payloads.append(payload) | ||
| index = len(self.payloads) - 1 | ||
| return self.responses[index] | ||
|
|
||
|
|
||
| class OpenAIAdapterSchemaRetryTest(unittest.TestCase): | ||
| def test_analyze_retries_once_when_schema_validation_fails(self): | ||
| invalid = _valid_response() | ||
| invalid["items"] = [invalid["items"][0], "},{"] | ||
| adapter = StubOpenAINewsletterAdapter( | ||
| [ | ||
| {"output_text": json.dumps(invalid, ensure_ascii=False)}, | ||
| {"output_text": json.dumps(_valid_response(), ensure_ascii=False)}, | ||
| ] | ||
| ) | ||
| request = NewsletterAnalysisRequest( | ||
| originalText="2026. 6. 15 수영 실기교육 안내", | ||
| language="KO", | ||
| referenceDate=date(2026, 6, 12), | ||
| ) | ||
|
|
||
| response = adapter.analyze(request) | ||
|
|
||
| self.assertEqual(response.title, "수영 실기교육 안내") | ||
| self.assertEqual(len(adapter.payloads), 2) | ||
| retry_messages = adapter.payloads[1]["input"] | ||
| self.assertIn("스키마 검증에 실패", retry_messages[-1]["content"]) | ||
| self.assertIn("items 배열의 모든 원소", retry_messages[-1]["content"]) | ||
| self.assertNotIn("},{", retry_messages[-1]["content"]) | ||
| self.assertIn('"loc": "items.1"', retry_messages[-1]["content"]) | ||
|
|
||
| def test_analyze_raises_after_retry_also_fails_validation(self): | ||
| invalid = _valid_response() | ||
| invalid["items"] = [invalid["items"][0], "},{"] | ||
| adapter = StubOpenAINewsletterAdapter( | ||
| [ | ||
| {"output_text": json.dumps(invalid, ensure_ascii=False)}, | ||
| {"output_text": json.dumps(invalid, ensure_ascii=False)}, | ||
| ] | ||
| ) | ||
| request = NewsletterAnalysisRequest( | ||
| originalText="2026. 6. 15 수영 실기교육 안내", | ||
| language="KO", | ||
| referenceDate=date(2026, 6, 12), | ||
| ) | ||
|
|
||
| with self.assertRaises(OpenAIAdapterError): | ||
| adapter.analyze(request) | ||
| self.assertEqual(len(adapter.payloads), 2) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.